# NYOJ 122 Triangular Sums

## Triangular Sums

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.

Each dataset consists of a single line of input containing a single integer n, (1 ≤ n ≤300), which is the number of points on a side of the triangle.

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

W(n)=1*T(2)+2*T(3)+3*T(4)+...+n*T(n+1)  ，    T(n)=1+2+3+4+...+n

#include<stdio.h>
#include<string.h>
int main(){
int n,mark=0,i;
scanf("%d",&n);
while(mark++<n){
int m,t=0,w[300];
scanf("%d",&m);
memset(w,0,sizeof(w));
printf("%d %d ",mark,m);
for(i=1;i<=(m+1);i++){
t+=i;
if(i>=2)
w[i-1]=w[i-2]+(i-1)*t;
}
printf("%d\n",w[m]);
}
return 0;
}

#include<iostream>
using namespace std;
const int M=310;
int W[M];
int main()
{
for(int i=1;i!=M;i++)
W[i]=W[i-1]+i*(i+1)*(i+2)/2;
int m,n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>m;
cout<<i<<" "<<m<<" "<<W[m]<<endl;
}
}



#### nyoj 122 triangular sums

2016-08-02 15:26:08

#### nyoj 122 Triangular Sums

2014-01-12 16:16:19

#### nyoj 122 Triangular Sums

2014-05-03 16:14:59

#### nyoj 122 triangular sums

2014-12-27 21:48:30

#### 【NYOJ】[122]Triangular Sums

2016-01-30 02:17:49

#### C 语言 南阳理工ACM 122.Triangular Sums

2013-08-22 16:35:44

#### 32-语言入门-32-Triangular Sums

2015-04-03 21:56:11

#### 南阳oj 题目122 Triangular Sums

2015-07-10 18:28:15

#### NYOJ--122--Triangular Sums

2016-08-02 20:04:34

#### 练习场 Triangular Sums

2018-02-08 19:37:03