1. 程式人生 > >杭電ACM1092求和問題詳解

杭電ACM1092求和問題詳解

/*
時間:
	20120314
作者:
	煙大洋仔
問題:
Problem DescriptionYour task is to Calculate the sum of some integers.

InputInput contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.

OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input. 

Sample Input4 1 2 3 45 1 2 3 4 50  

Sample Output1015
解析:
	該題目主要是要控制輸入資料的個數,通過while進行控制輸入的資料並	且判斷
	程式是不是應該結束;
		第二個while語句為控制輸入資料的個數,並且計算出要求得和	;
	第一遍的時候出現了錯誤;
	因為sum在第二次計算之前沒有清零!!!!這點很重要

*/
#include <iostream>
using namespace std;
int main()
{
    int n,a,sum=0;
    while(cin>>n&&n!=0)
    {
        while (n--)
        {
        cin>>a;
        sum=sum+a;
        }
        cout<<sum<<endl;
        sum=0;
    }

    return 0;
}