Your task is to calculate the sum of some integers.
标签:
Your task is to calculate the sum of some integers.
Input 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.
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
4 1 2 3 4
5 1 2 3 4 5
0
10
15
[思路] 每组数前面有个组中的数字的个数,先读这个n,判断一下是不是0,后面的数就用一个for循环读进去累加起来就好。记得一开始的初始值要设0,不然会累加出来奇怪的东西。
[代码]
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int n; 7 while(cin >> n && n) 8 { 9 int sum = 0; int temp = 0; 10 for(int i = 0 ; i < n ; i++){cin >> temp ; sum += temp;} 11 cout << sum << endl; 12 } 13 return 0; 14 }
标签:
原文地址:http://www.cnblogs.com/ticsmtc/p/5350564.html