/* Sample Input 4 3 4 5 10 Sample Output 1 3 45 2 4 105 3 5 210 4 10 2145 The Solution: 1. First, we read in all datasets. From the input, we can find out the maximum n for which we are going to calculate W(n). For the sample input, n is 10. 2. According to definitions, T(n)=T(n-1)+n, W(n)=W(n-1)+n*T(n+1), so we can incrementally calculate T(1),T(2),....,T(n),T(n+1) and WT(1), WT(2), ..., WT(n). For the sample input, we have WT(1),...,WT(10). 3. Then we have the results for all datasets. For the sample input, we get W(3),W(4),W(5),W(10). 4. Then we can print out the results according to the input order of datasets. */ /* The Triangular Sum Problem Given a number n (>0), 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)] Input 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. Output 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. Sample Input 4 3 4 5 10 Sample Output 1 3 45 2 4 105 3 5 210 4 10 2145 */ #include "stdafx.h" #include <cassert> #include <iostream> #include <vector> using namespace std; namespace wt_triangular_sum { void wt_triangular_sums() { vector<int> datasets; //read input into a vector int maxside=0; int count; int side; cin>>count; while(count>0) { cin>>side; if(side<=0) { cout<<"illegal number, please enter a positive number!"<<endl; continue; } datasets.push_back(side); if (side>maxside) maxside = side; count--; } if (datasets.size()==0) return; //calculate all W(n) for n=1,2,...,maxside int tk_1=1; //represent T(k+1) for k=0,1,2,3,... vector<int> wt(maxside); //wt[i] represents W(i+1) i=0,1,2,...,maxside-1 for (int i=0; i<maxside; i++) { int k=i+1; tk_1 += k+1; //tk_1 represents T(k+1), k from 1,2,...,maxside wt[i] = (i==0)?k*tk_1:(wt[i-1]+k*tk_1); //wt[i] represents wt[k] } //print out results for (int i=0; i!=datasets.size(); i++) cout<<(i+1)<<" "<<datasets[i]<<" "<<wt[datasets[i]-1]<<endl; } }