1089
Input:The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
输入不说明有多少个input block ,以EOF为结束标志。
解决方法:
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)!=EOF)
printf("%d\n",a+b);
return 0;
}
while(scanf("%d %d",&a,&b)!=EOF)
{
....
}
1090
Input:Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.
输入一开始说会有N个input block,下面接着N个input block。
解决方法:
#include<stdio.h>
int main()
{
int n,i,a,b;
sacnf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&a,&b);
printf("%d\n",a+b);
}
return 0;
}
scanf("%d",&n);
for(i=0;i<n;i++){
...
}
1091
Input:Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.
输入不说明有多少个inout block,但以某个特殊输入作为结束标志。
解决方法:
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)!=EOF&&(a!=0||b!=0))
printf("%d\n",a+b);
return 0;
}
while(scanf("%d %d",&a,&b)!=EOF&&(a!=0||b!=0))
{
.....
}
综合情况
1092
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.
开始先说明N个input block,并以N的某个特殊输入作为结束标志。
解决方法:
#include <stdio.h>
int main(void)
{
int a, n, i, sum;
while(scanf("%d", &n) && n != 0) //while(scanf("%d", &n) != 0)是错误的用法
{
sum = 0;
for(i = 0; i < n; i ++)
{
scanf("%d", &a);
sum = sum + a;
}
printf("%d\n", sum);
}
return 0;
}
1093
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.
用常规的方法编写易超时
解决方法
#include <stdio.h>
int main()
{
int n;
int m,a,sum;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
scanf("%d",&m);
sum=0;
while(m--)
{
scanf("%d",&a);
sum+=a;
}
printf("%d\n",sum);
}
}
return 0;
}
1094
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.
解决方法
#include<stdio.h>
int main()
{
int n,a,sum;
while(scanf("%d", &n)!=EOF)
{
sum=0;
while(n--)
{
scanf("%d" , &a);
sum += a;
}
printf("%d\n",sum);
}
return 0;
}
1095
Output:For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.
解决:printf("%d\n\n",a+b);
1096
注意给出的样例,最后一个结果后只有一个 blank line
#include<stdio.h>
int main()
{
int n,a[100],sum,m,i;
scanf("%d",&n);
while(n--)
{
sum=0;
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
if(n!=0)
printf("%d\n\n",sum);
if(n==0)
printf("%d\n",sum);
}
}