算法问题

沉默的牧羊者 2015-04-25 04:49:07
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


这是一道ACM的训练题

以下是我的的代码

#include <iostream>

using namespace std;

int main()

{
int T;
cin >> T;
int T1 = T;
int a[1005],t;
int i,j = 0;
long long s[15] = { 0 };
while (T--)
{
cin >> t;
for (i = 0; i < t; i++)
{
cin >> a[i];
s[j] += a[i] * a[i] * a[i];
}
j++;
}
i = 0;
while (T1--)
{
cout << s[i] << endl;
i++;
}
return 0;
}
我调试的时候没有发现错误,内存方面longlong是支撑的起的,但是答案就是错的,不清楚为什么。
...全文
66 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
FancyMouse 2015-04-25
  • 打赏
  • 举报
回复
s[j] += a[i] * a[i] * a[i]; 你s是long long了,但是a还是int,还是溢出
苏叔叔 2015-04-25
  • 打赏
  • 举报
回复
看不出哪儿错了,稍作修改:

#include <iostream>
using namespace std;
int main()
{
	int T;
	cin >> T;
	int T1 = T;
	//int a[1005], t;
	int a[1000], t;
	int i, j = 0;
	//long long s[15] = { 0 };
	long long s[10] = { 0 };
	while (T--)
	{
		cin >> t;
		for (i = 0; i < t; i++)
		{
			cin >> a[i];
			s[j] += a[i] * a[i] * a[i];
		}
		j++;
	}
	i = 0;
	while (T1--)
	{
		cout << s[i] << endl;
		i++;
	}
	return 0;
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧