算法问题
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是支撑的起的,但是答案就是错的,不清楚为什么。
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是支撑的起的,但是答案就是错的,不清楚为什么。
...全文
相关推荐
算法设计TSP问题的四种算法实现
**算法设计:解决TSP问题的四大策略**在信息技术领域,优化问题的解决方案往往依赖于高效算法的设计。
TSP问题三种算法
该项目实现了求解旅行商问题的三种算法,包含贪心、动态规划与遗传算法。核心组件包括Bee类模拟人工蜂群、GNode类构建搜索节点树及Form1窗体进行可视化交互。代码通过适应度评估与路径优化策略,完成T
组合优化问题及算法
根据给定的标题“组合优化问题及算法”和描述“很不错的哦”,我们可以看出这篇文章主要聚焦于介绍组合优化问题及其解决方法。
装载问题回溯算法
回溯算法是一种试探性的解决问题的方法,它通过递归地尝试所有可能的解决方案来寻找最优解。
经典问题算法解法汇总
在IT领域,算法是解决问题的核心工具,特别是在计算机科学和软件工程中。"经典问题算法解法汇总"这个主题涵盖了多种常见的算法,它们是解决特定问题的有效途径。


