当前位置:网站首页>向量3(静态成员)
向量3(静态成员)
2022-06-22 14:09:00 【SZU治愈系BUG】
目录
题目描述
为向量1题目实现的CVector类添加私有静态成员sum,在初始化对象的同时,统计所有对象的n维向量和sum。
主函数生成多个对象,测试向量和。
可根据需要自行添加需要的静态成员函数,添加非静态成员函数不得分。
输入
测试次数t
每组测试数据格式如下:
输入m,表示n维向量的数目
后跟m行,每行格式:向量维数n n维向量值
输出
对每组测试数据的m个向量,输出所有向量的分量和sum
输入样例1
2
2
5 1 2 3 4 5
3 4 5 6
3
2 1 2
3 10 20 30
2 11 22
输出样例1
1 2 3 4 5
4 5 6
30
1 2
10 20 30
11 22
96
AC代码
#include<iostream>
using namespace std;
class CVector
{
int * data;
int n;
static int sum;
public:
CVector(){
data=new int[5];
for(int i=0;i<5;i++)
{
data[i]=i;
sum+=i;
}
n=5;
}
CVector(int n1,int *a){
n=n1;
data=new int[n1];
for(int i=0;i<n1;i++)
{
data[i]=a[i];
sum+=a[i];
}
}
void display(){
int i;
for(i=0;i<n-1;i++)
cout<<data[i]<<' ';
cout<<data[i]<<endl;
}
static int getsum(){return sum;}
static void setsum(){sum=0;}
~CVector(){
if(data)
delete[] data;
data=NULL;
}
};
int CVector::sum=0;
int main() {
int n,i,t,m;
cin>>t;
while(t--)
{
CVector::setsum();
cin>>m;
while(m--)
{
cin>>n;
int *p=new int [n];
for(i=0;i<n;i++)
cin>>p[i];
CVector b(n,p);
b.display();
if(p)
delete[] p;
p=NULL;
}
cout<<CVector::getsum()<<endl;
}
}边栏推荐
- Sword finger offer46 -- translate numbers into strings
- Detailed explanation of CSAPP Labs
- What are the five characteristics of network security? What are the five attributes?
- RealNetworks vs. 微软:早期流媒体行业之争
- Reconstruction practice of complex C-end project of acquisition technology
- Keil simulation and VSPD
- 网络安全的五大特点有哪些?五大属性是什么?
- 同花顺开户难么?网上开户安全么?
- 树结构二叉树
- 【直播回顾】战码先锋第六期:共建测试子系统,赋能开发者提
猜你喜欢

树结构二叉树

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!

极致效率,云原生数据库TDSQL-C安身立命的根本

Database connection pool: stress testing
![[Software Engineering] design module](/img/08/d55af729a8241e109fdeb96c896670.png)
[Software Engineering] design module

接了个私活项目,一下赚了15250,还有必要做主业吗?

At 19:00 this Thursday evening, the 7th live broadcast of battle code Pioneer - how third-party application developers contribute to open source

UE4 通过蓝图获取本地文件

What happened to those who didn't go to college

全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer
随机推荐
同花顺如何开户?在线开户安全么?
What are strong and weak symbols in embedded systems?
晒晒我这两年的私活单,业余时间月入6k,有份副业也太香啦
树结构二叉树
Ros2 pre basic tutorial | using cmakelists Txt compile ros2 node
bochs 软件使用记录
PowerPoint 教程,如何在 PowerPoint 中添加水印?
What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
Method of using inout signal in Verilog
Follow up to the "June 21" gas explosion accident in Tai'an, Shandong Province: comprehensive investigation and remediation of potential gas safety hazards in restaurants
三菱机械臂demo程序
专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
How to open an account in flush? Is online account opening safe?
Charles 乱码问题解决
【毕业设计】基于半监督学习和集成学习的情感分析研究
RealNetworks vs. Microsoft: the battle in the early streaming media industry
数据库连接池:代码目录
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
ML笔记-matrix fundamental, Gradient Descent
PowerPoint tutorial, how to add watermarks in PowerPoint?