码迷,mamicode.com
首页 > 其他好文 > 详细

[1003] A + B Problem (3)

时间:2016-04-03 21:58:44      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

1003: A+B Problem(4)

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 487  Solved: 242
[Submit][Status][Web Board]

Description

Your task is to calculate the sum of some integers.

Input

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.

Output

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.

Sample Input

4 1 2 3 4
5 1 2 3 4 5
0

Sample Output

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 }

 

[1003] A + B Problem (3)

标签:

原文地址:http://www.cnblogs.com/ticsmtc/p/5350564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!