您的位置:首页 > 其它

acm_step1.1.4

2017-06-18 15:29 369 查看
题目描述:

Problem 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,表示将要计算n个数的和,若n为0,则不作任何处理。

#include<stdio.h>

int main()
{
int n, a, i;
int sum = 0;

while (scanf("%d", &n) != EOF)
{
if (n != 0)
{
for (i = 0; i < n; i++)            /*从0开始输入n个数*/
{
scanf("%d", &a);
sum += a;                      /*每次输入后都把值加到sum上*/
}
printf("%d\n", sum);
sum = 0;                            /*每次结束后都把sum重置为0*/
}
}
return 0;
}


难点:对i从0到n-1时的处理,不太容易计算每次输入的和,容易忽略每次加后,对sum置0.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm