Programming Question # #: Calculate the sum of squares
Source: Peking University Online Program evaluation system POJ (Coursera statement: The exercises completed on POJ will not count to the final results of Coursera. )
Total time limit: 1000ms memory limit: 1024kB
Describe
Write the sum function so that it can calculate the sum of squares of the input series.
#include <iostream>using namespace std;//here to supplement your code int sqr (int n) { return n * N;} int main () { int t, n, a[0x100]; CIN >> T; for (int c = 0; c < T; ++c) { cin >> N; for (int i = 0; i < n; ++i) cin >> A[i]; cout << sum (A, n, Sqr) << Endl; } return 0;}
Input
The first line is an integer t (T <= 10), representing the number of data groups;
Each set of input data contains two rows, the first line is an integer n (n <= 100),
The second line is n spaces separated by a number of integers
Output
For each set of input data, output the sum of the squares of n integers in the group data
Sample input
224 330 1 2
Sample output
255
#include <iostream>using namespacestd;//Add your code hereintSumintA[],intNint(*SQR) (int)) { intsum =0; for(inti =0; I < n; ++i) {sum+=Sqr (A[i]); } returnsum;}intSqrintN) {returnn *N;}intMain () {intT, N, a[0x100]; CIN>>T; for(intc =0; C < T; ++c) {cin>>N; for(inti =0; I < n; ++i) Cin >>A[i]; cout<< sum (A, n, Sqr) <<Endl; } return 0;}
POJ C + + programming Problem # # #: Calculating the sum of squares