POJ C + + programming Problem # # #: Calculating the sum of squares

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.