关于if(a)的思考与杭电oj1.1.8

最近经常看到 if(a),while(a),while(!a),if(!a),if(i%2)
①if(a)和while(a)
在计算机中 要么真(非0值)要么假(值为0)
再来看if(a)和while(a)
如果不等0一直循环,等于0则结束。
②while(!a)和if(!a)
这里的!a与a相反,表示如果等于0的话循环,不等于零则结束。
③if(i%2)
表示如果i是奇数的话执行if语句,即不能被2整除的数循环if语句。
在这里插入图片描述
在这里插入图片描述
1.1.8
Problem Description
Your task is to calculate the sum of some integers.
Input
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.
Output
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 N;
scanf("%d",&N);
while(N–)
{
int i,x,m,s=0;
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d",&x);
s=s+x;
}
printf("%d\n\n",s);
if(N)//★要想每行输出数据之间有一个空格行就需要考虑一下格式问题。
printf("\n");//★if(N)每次输入N都减小1,直到减小到0的时候if(N==0)停止循环
} //★最后一个数据输出结束后不需要输出空格行。
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43787387/article/details/86543901
今日推荐