当前位置:网站首页>Vector 3 (static member)

Vector 3 (static member)

2022-06-23 01:04:00 SZU healing system bug

Catalog

Title Description

AC Code


Title Description

Vector 1 Topic realization CVector Class to add private static members sum, While initializing the object , Statistics of all objects n Dimensional vector sum sum.

The main function generates multiple objects , Test vectors and .

You can add static member functions as needed , Adding non static member functions does not score .

Input

Number of tests t

The format of each group of test data is as follows :

Input m, Express n The number of dimensional vectors

Heel m That's ok , Format per line : Vector dimension n n Dimension vector values

Output

Of each set of test data m Vector , Output the sum of the components of all vectors sum

sample input 1 

2
2
5 1 2 3 4 5
3 4 5 6
3
2 1 2 
3 10 20 30
2 11 22

sample output 1

1 2 3 4 5
4 5 6
30
1 2
10 20 30
11 22
96

AC Code

#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;
	}
}
原网站

版权声明
本文为[SZU healing system bug]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221409100453.html