Triangular Sums
时间限制:3000 ms | 内存限制:65535 KB
难度:2
描述:
The nth Triangular number, T(n) = 1 + … + n, is the sum of the first n integers. It is the number of points in a triangular array with n points on side. For example T(4):
X
X X
X X X
X X X X
Write a program to compute the weighted sum of triangular numbers:
W(n) = SUM[k = 1…n; k * T(k + 1)]
输入:
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
输出:
For each dataset, output on a single line the dataset number (1 through N), a blank, the value of n for the dataset, a blank, and the weighted sum ,W(n), of triangular numbers for n.
样例输入:
4
3
4
5
10
样例输出:
1 3 45
2 4 105
3 5 210
4 10 2145
来源
Greater New York 2006
上传者
张云聪
题目网址: (http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=122)
翻译:
第n个三角形数T ( n ) = 1 +…+ n是前n个整数的和。它是边上有n个点的三角形数组中的点数。例如T ( 4 ) :
X
X X
X X X
X X X X
编写一个计算三角数加权和的程序:
W ( n ) =SUM[ k = 1…n;k*T( k + 1 ) ]
输入
第一行包含单个整数N ( 1≤N≤1000 ),它是随后的数据集数。
输出
每个数据集由包含单个整数n ( 1≤n≤300 )的单行输入组成,该整数n是三角形边上的点数。对于每个数据集,在单行上输出数据集编号( 1到N )、空格、数据集的值N、空格以及三角形编号的加权和W ( N )
题意:
T(n)=1+…+n;W(n)=SUM[ k = 1…n;k*T( k + 1 ) ];k是从1到n;W(n)等于从1到n中k*T(k+1).
思路:
先求出T(n)=n*(n+1)/2;再求k*T(k+1)=k*(k+1)*(k+2)/2;最后用循环求和即可。
代码:
#include<iostream>
using namespace std;
int main()
{
int n,x,s,i,j,q;
cin>>n;
for(i=1;i<=n;i++)
{
s=0;
cin>>x;
for(j=1;j<=x;j++)
{
q=j*(j+1)*(j+2)/2;
s+=q;
}
cout<<i<<' '<<x<<' '<<s<<endl;
}
return 0;
}
运算结果:
总结:
这个题其实是个简单题,主要在于翻译和理解题意上。