1095如题
题目解析:
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
大致意思是:输入一个N表示进行多少次求和,每一行第一个数表示对接下来的M个整数求和
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.
大概意思是:将每一个和进行空行输出,即每个结果之间空一行
逐步实现代码
注意
这里需要注意的是,在最后一次输出之后不需要再进行空行处理
#include<stdio.h>
int main()
{
int m, n, sum, i; //初始化数值,可以留到最后再定义
while(scanf("%d",&n)!=EOF) //首先利用 while 获取求和次数n
{
while(n--) //用while循环开始求和
{
sum = 0; //初始化 sum,因为要多次求和
scanf("%d",&m); //获得M即求和数个数
while(m--) //m 个数求和
{
scanf("%d",&i); //获取元素
sum += i; //求和
}
if(n == 0) //两个输出行之间进行空行处理,这里需要注意的是
printf("%d\n",sum); //在最后一次输出之后不需要再进行空行处理,所以用到了if语句
else
printf("%d\n\n",sum);
}
}
}
这一题将1093进行了变换,在输出方面做了一点改动
其中while(scanf(“%d%d“,&a,&b)!=EOF)用法详见ACM-while(scanf(“%d%d“,&a,&b)!=EOF)