本文主要帮助刚接触ACM赛制的同学熟悉并理解常见的比赛输入输出格式
由于ACM采用逐字符匹配来判断答案的正确性,所以对格式要求十分严格,为避免思路正确却因为格式问题遭到罚时,需要深刻理解比赛时的数据输入输出与处理。
1、EOF理解
2、while()循环灵活应用
所有题目链接
Your task is to Calculate a + b.Too easy?! Of course! I specially designed the problem for acm beginners.You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
输入ab两个数字,计算a+b每个答案占一行。
由于不清楚具体组数,所以需要判断是否到达文件末尾,主要考大家懂不懂多组输入,理解文件末尾为EOF这个事实。
#include
int main()
{
int a,b;
//while(~scanf("%d%d",&a,&b))这两个while语句是等价的
while(scanf("%d%d",&a,&b)!=EOF)//当文件输入到末尾时scanf返回值为 EOF(end of file)意味着多组数据处理结束
{
printf("%d\n",a+b);//每次答案换一行
}
}
Your task is to Calculate a + b.
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
输入n组数据,输出a+b每个答案占一行
只需要一个值首先获取组数即可
#include
main()
{
int a,b,t;
scanf("%d",&t);
while(t--)
scanf("%d%d",&a,&b),printf("%d\n",a+b);
}
Your task is to Calculate a + b.
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.
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
不确定组数据,输出a+b每个答案占一行,0 0不做处理
将while循环里的EOF判断条件换为a与b其中一个不为0.
#include
main()
{
int a,b;
while(~scanf("%d%d",&a,&b)&&(a||b))//while(~scanf("%d%d",a+b))
//a&&b Wrong
//if(a==0&&b==0)break;
printf("%d\n",a+b);
}
Your task is to Calculate a + b.
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.
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.
不确定组数据,首先输入一个n代表一行数的个数,如果为0 不做处理,否则读入n个数,求和输出
判断n是否为0,若为0跳出循环。
#include
main()
{
int a,n,s;
while(scanf("%d",&n),n)
{
s=0;
while(n--)
scanf("%d",&a),s+=a;
printf("%d\n",s);
}
}
Your task is to Calculate a + b.
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.
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.
确定组数数据,首先输入一个t代表有t组数据,再读入m,依次读入m个数。累加求和
按照题目分析照做就好
#include
main()
{
int t,n,x,s;
scanf("%d",&t);
while(t--)
{
s=0;
scanf("%d",&n);
while(n--)
scanf("%d",&x),s+=x;
printf("%d\n",s);
}
}
Your task is to Calculate a + b.
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.
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.
不确定组数数据,多组数据输入,首先输入一个n代表一行有n个数据,读入n个数,求和输出
判断输入是否为EOF
#include
main()
{
int n,x,s;
while(~scanf("%d",&n))
{
s=0;
while(n--)
scanf("%d",&x),s+=x;
printf("%d\n",s);
}
}
Your task is to Calculate a + b.
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.
不确定组数数据,多组数据输入,输出a+b每次输出两行
判断输入是否为EOF,注意输出两行
#include
main()
{
int a,b;
while(~scanf("%d%d",&a,&b))
printf("%d\n\n",a+b);
}
Your task is to Calculate a + b.
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.
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.
hint:last line don‘t follow blank line
确定组数数据,输出a+b每次输出两行最后不要输出空行
对最后一组数据进行特判断,显然t!=1时输出两行其余不输出。
#include
main()
{
int t,n,x,s;
scanf("%d",&t);
while(t--)
{
s=0;
scanf("%d",&n);
while(n--)
scanf("%d",&x),s+=x;
printf("%d\n",s);
if(t!=0)
puts("");
}
}
主要分为两种题型:
1.确定组数 ,使用while(t–)可以快速处理,若要求最后一行不输出空格显然t==1时为最后一组
2.不确定组数,分为判断EOF与特殊数据处理,只需要将break 条件放置while()中即可。