1006: A+B 结束标志 scanf("%d", &n) == EOF (VI)

版权声明:看看就好,如需转载,注明一下出处 https://blog.csdn.net/sinat_34337520/article/details/89346105

Description

Your task is to calculate the sum of some integers.

Input

Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.

Output

For each test case you should output the sum of N integers 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

Sample Output

10
15

#include <stdio.h>

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

    while(1)
    {
        if(scanf("%d", &n) == EOF)
            break;
        for(i=0; i<n; i++)
        {
            scanf("%d", &a[i]);
            sum += a[i];
        }
        printf("%d\n", sum);
        sum = 0;
    }
    return 0;
}

6178753-545938280539ec7b.png

运行截图.png

Note:

题目丝毫没有提及如何结束输入,OJ默认读者知道是用 EOF 结束。
scanf函数返回成功读入的数据项数,读入数据时遇到了“文件结束”则返回EOF

猜你喜欢

转载自blog.csdn.net/sinat_34337520/article/details/89346105
今日推荐