A - 一道难题
Problem Description
Give you n elements a1, a2, a3,..., an.You need to calculate the sum of (a1 * a1 * a1 + a2 * a2 * a2 + ... + an * an * an).
Input
The first line of the input contains an integer T(1<=T<=10) which means the number of test cases.
Each case starts with a line containing a integers n(1 <= n <= 1,000).
The next line contains n space-separated elements ai (1 <= i <= n), and 0 <= ai <= 100,000.
Output
Sample Input
2 2 1 1 3 2 4 7
Sample Output
2 415
Hint
The answer maybe very large.
unsigned int 0 ~ 4294967295 (2^32 - 1)
int -2147483648 ~ 2147483647 (2^31 - 1)
unsigned long 0 ~ 4294967295 (2^32 - 1)
long -2147483648 ~ 2147483647 (2^31 - 1)
unsigned long long 0 ~ 18446744073709551615 (2^64 - 1)
long long -9223372036854775808 ~ 9223372036854775807 (2^63 - 1)
Here is a sample solution to problem a+b which has T cases using C/C++:
#include <iostream>
using namespace std;
int main()
{
int T; cin >> T;
while(T--) {
int a,b;
cin >> a >> b;
cout << a+b << endl;
}
return 0;
}
Or:
#include <cstdio>
using namespace std;
int main()
{
int T; scanf("%d", &T);
while(T--) {
int a,b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
}
return 0;
}
这道题主要考的就是数据的存储范围,需要考虑用什么类型的数据来存储,由于系统只能接 long long ,所以以下代码用的是 long long,在其他的系统可能也可以用 _int64
#include<iostream>
//#include<string>
using
namespace
std;
int
main()
{
unsigned
int
T;
unsigned
int
n;
double
num;
long
long
sum; //系统默认接受long long ,调试时你可能发现long long是语法错误,因为现在的电脑一般接受 _int64 了
cin>>T;
while
(T--) //循环T次
{
sum=0;
cin>>n;
for
(
int
i=0;i<n;++i)
{
cin>>num;
if
(num<0||num>100000)
break
;
sum+=num*num*num;
}
if
(num<0||num>100000)
break
;
cout<<sum<<endl;
}
return
0;
}