对于ACM 比赛中,输入输出的模板:
1) 第一行是输入组的个数,后面连续的行为每组的输入参数的情况:(如A+B)
3
10 11
1 2
4 5
第一行3表示有3组测试数据,第二行到第四行表示不同的测试样例:(C++)
#include<iostream>
using namespace std;
int main()
{
int n,a,b;
cin>>n;
while(n--!=0)
{
cin>>a>>b;
cout<<a+b<<endl;
}
return 0;
}
如果是C版本, 为:
#include<iostream>
using namespace std;
int main()
{
int n,a,b;
scanf("%d",&n);
while(n--!=0)
{
scanf("%d%d",&a,&b);
printf("%d\n",a+b);
}
return 0;
}
2) 每行为每组的输入参数的情况:(如A+B)
10 11
1 3
2 5
从第一行到第三行为输入数据:
C++版:
#include<iostream>
using namespace std;
int main()
{
int a,b;
while( cin>>a>>b)
{
cout<<a+b<<endl;
}
return 0;
}
C版:
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d%d",&a,&b) == 2)
{
printf("%d\n",a+b);
}
return 0;
}
其中,scanf("%d%d",&a,&b) == 2 表示按照指定格式 %d%d正确读入数据的个数。
3)后面有一个输入结束条件,例如A+B
每组输入单独占一行,当输入为0 0 时,输入结束, 0 0 这组数据不做处理:
例如输入:
1 2
3 4
10 20
0 0
C++ 版:
#include<iostream>
using namespace std;
int main()
{
int a,b;
while( cin>>a>>b && (a||b))
{
cout<<a+b<<endl;
}
return 0;
}
C版:
</pre><p><pre name="code" class="cpp">#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d%d",&a,&b) == 2 && (a||b))
{
printf("%d\n",a+b);
}
return 0;
}
4) 不同结束符
题目:多个数求和: A+B+C+...
输入:每个测试样例包含一个正整数N,随后是N个输入数据
当某个测试样例以0开始时,终止输入,不处理该测试样例
例如:
3 1 2 4
1 23
5 1 3 5 7 9
输出:
7
23
25
C++版:
#include<iostream>
using namespace std;
int main()
{
int n,t,s;
while(cin>>n && n!=0)
{
s = 0;
while(n--!=0)
{
cin>>t;
s = s+ t;
}
cout<<s<<endl;
}
return 0;
}
C 版:
#include<stdio.h>
int main()
{
int n,t,s;
while(scanf("%d",&n)== 1 && n!=0)
{
s = 0;
while(n--!=0)
{
scanf("%d",&t);
s = s+ t;
}
printf("%d\n",s);
}
return 0;
}