acm_step1.1.4

来源:互联网 发布:淘宝男士家居服 编辑:程序博客网 时间:2024/05/20 08:23

题目描述:
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.

原创粉丝点击