当前位置:网站首页>向量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;
}
}边栏推荐
- 难怪考研热度这么高,这些是研究生才有的“隐藏福利”!
- 好风凭借力 – 使用Babelfish 加速迁移 SQL Server 的代码转换实践
- Those confusing user state & kernel state
- [Zhejiang University] information sharing of the first and second postgraduate entrance examinations
- Vscode个性化设置:让一个小萌妹陪你敲代码
- 嵌入式中的强符号和弱符号是什么?
- bochs 软件使用记录
- [Software Engineering] acquire requirements
- 剑指Offer46——把数字翻译成字符串
- 先锋期货靠谱么?期货怎么开户安全些?
猜你喜欢

Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!

2022年失业的人多吗?今年是不是特别难找工作?

曾经,我同时兼职5份工作,只为给女友买个新款耳环......

FreeRTOS task priority and interrupt priority

How MySQL modifies a field to not null

The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation

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

Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?

Common operations in Visual Studio development

Using virtual serial port to debug serial port in keil MDK
随机推荐
乱解码nlp
OpenVINO CPU加速调研
先锋期货靠谱么?期货怎么开户安全些?
润迈德医疗通过聆讯:年内亏损6.3亿 平安资本是股东
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
What happened to those who didn't go to college
【毕业设计】基于半监督学习和集成学习的情感分析研究
世界上所有的知名网络平台
phpStudy 2016搭建-pikachu靶场
Struggle, programmer chapter 50: a bosom friend in the sea
阿里云发布CIPU,对于企业客户意味着什么?
FPGA采集DHT11温湿度
Thoroughly understand the builder mode (builder)
No wonder the postgraduate entrance examination is so hot. These are the "hidden benefits" of Postgraduates!
接了个私活项目,一下赚了15250,还有必要做主业吗?
谷歌竞价账户可以探测到全球市场吗?
我靠副业一年全款买房:那个你看不起的行业,未来十年很赚钱!
网站存在的价值是什么?为什么要搭建独立站
树结构二叉树
mysql如何将字段修改为not null