您的位置:首页 > 编程语言 > PHP开发

A+B for Input-Output Practice (IV)

2020-03-30 07:34 1211 查看

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

C

#include <stdio.h>
#pragma warning(disable:4996)
int main(void)
{
int sum,input,i,n;
while(scanf("%d",&n)&&n)
{
for(i=0,sum=0;i<n;i++)
{
scanf("%d",&input);
sum+=input;
}
printf("%d\n",sum);
}
return 0;
}

每行第一个数字表示接下来要给的数字数量,写在while循环里。
然后for循环读入剩余的数字并相加,for循环结束后输出和。
注意:
C语言用除0以外的任何数表示真,所以在n=0时,while循环结束。
每次计算后要把sum的值归0。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
coffee__a 发布了28 篇原创文章 · 获赞 4 · 访问量 1778 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: