广州大学第九届ACM A——一道难题

来源:互联网 发布:windows samba服务器 编辑:程序博客网 时间:2024/04/29 18:20



A - 一道难题

Time Limit: 2000/1000 MS (Java/Others)      Memory Limit: 128000/64000 KB (Java/Others) 
Submit Status

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

For each case, you need to print the answer in a single line. 

Sample Input

221 132 4 7

Sample Output

2415

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>
usingnamespace std;
 
intmain()
{
    unsigned intT;
    unsigned intn;
    doublenum;
    longlong sum;      //系统默认接受long long ,调试时你可能发现long long是语法错误,因为现在的电脑一般接受 _int64 了
    cin>>T;
    while(T--)         //循环T次
    {
        sum=0;
        cin>>n;
        for(inti=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;
 
    }
 
    return0;
}



0 0