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



A - 一道难题

Time Limit:  2000/1000 MS (Java/Others)       Memory Limit:  128000/64000 KB (Java/Others) 
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
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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值