转载http://blog.youkuaiyun.com/u010480899/article/details/52280065
由于ACM竞赛题目的输入数据和输出数据一般有多组(不定),并且格式多种多样,所以,如何处理题目的输入输出是对编程者最基本的要求。
(一)第一类输入:输入不说明有多少个Input Block,以EOF为结束标志。
Example1:
Calculate A + B .
Input:Each line will contain two integers A and B . Process to end of file.
Output:For each case, output A + B in one line.
Sample Input
1 1
2 2
Sample Output
2
4
这个例子的意思是要每读取两个数就将两个数的和进行输出,输入的解决方法如下:
C语法:
while(scanf("%d %d",&a, &b) != EOF)
{
//放入计算和的语句即可
}
C++语法:
while( cin >> a >> b )
{
//放入计算和的语句即可
}
注:scanf函数返回值就是读出的变量个数,如:scanf( “%d %d”, &a, &b );
如果只有一个整数输入,返回值是1,如果有两个整数输入,返回值是2,如果一个都没有,则返回值是-1。EOF是一个预定义的常量,等于-1。
在有的比赛场合,测试数据的并不是由后台通过cin和cout输入而是要去读文件,这里就需要了解和掌握C++读写文件的操作。在C++中对一个文件进行读写需要先打开一个文件,然后使用fin和fout进行读写,在完成所有数据的读写操作后要关闭文件。下面是C和C++读写文件的操作,还是以上面计算a+b为例,看看C++和C是怎样操作文件流的。
C++语法:
#include<iostream>
#include<fstream>
using namespace std;
void main()
{
ifstream fin("in.txt");
ofstream fout("out.txt");
int a,b;
while(fin>>a>>b)
fout<<a+b<<endl;
fin.close();
fout.close();
}
C语法:
#include <stdio.h>
void main()
{
FILE *fin,*fout;
int a,b;
fin=fopen("in.txt","r");
fout=fopen("out.txt","w");
while(fscanf(fin,"%d %d",&a, &b) != EOF)
fprintf(fout,"%d\n",a+b);
fclose(fin);
fclose(fout);
}
(二)第二类输入:题目告诉有N个Input Block,下面接着是N个Input Block。
Example2:
Problem Description
Your task is to Calculate a + b.
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.
Output:
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.
Sample Input:
2
1 5
10 20
Sample Output:
6
30
在这个题目中明确告知了输入数据有2组,然后每一组代表需要计算和的两个加数,因此只需要进行两次循环即可。这种类型的输入数据方式如下:
C语法:
scanf("%d",&n) ;
for( i=0 ; i<n ; i++ )
{
scanf("%d %d",&a, &b);
printf(“%d\n”,a+b);
}
C++语法:
cin >> n;
for( i=0 ; i<n ; i++ )
{
cin>>a>>b;
cout<<a+b<<endl;
}
(三)第三类输入:输入不说明有多少个Input Block,但以某个特殊输入为结束标志。
Example3:
Problem Description
Your task is to Calculate a + b.
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.
Output
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.
Sample Input
1 5
10 20
0 0
Sample Output
6
30
这里题目中也是计算a和b的和,每次读取两个数,然后输出和,但是至于有多少组数据需要输入则是由特殊的输入确定,在该题目中当读入的两个数都为零时,则认为数据的输入结束。
C语法:
#include <stdio.h>
int main()
{
int a,b;
while(scanf("%d %d",&a, &b) &&(a!=0 && b!=0))
printf("%d\n",a+b);
}
C++语法:
#include<iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
if(a==0 && b==0)
break;
cout<<a+b<<endl;
}
return 0;
}
对于第三类输入输出,通常的解决方案如下,至于遇到何种结束方式,这根具体的题目有关,但是通常此类输入解法的方法如下,只需要根据具体的结束规则稍加改动即可。
C语法:
while(scanf("%d",&n) !=EOF && n!=0 )
{
}
C++语法:
while( cin >> n && n != 0 )
{
}
(四)第四类输入:上述三种的组合
Example4:
Your task is to Calculate the sum of some integers.
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.
Output:
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.
Sample Input:
4 1 2 3 4
5 1 2 3 4 5
0
Sample Output
10
15
这个题目同样是计算a+b的和,但是数据的输入格式是这样的,先输入N,代表后面有N个数据进行相加,当N为0时,表示输入结束。因此,本题目中的输入格式如下:
#include<iostream>
using namespace std;
int main()
{
int i,n,s,sum;
while(cin>>n && n!=0)
{
sum=0;
for(i=0; i<n; i++)
{
cin>>a;
sum+=a;
}
cout<<sum<<endl;
}
return 0;
}
(五)第五类输入:输入是一整行的字符串,本类的输入方式如下:
C语法:
char buf[20];
gets(buf);
C++语法:
char buf[ 255 ];
cin.getline( buf, 255 );
注:在C中canf(“%s%s”,str1,str2),在多个字符串之间用一个或多个空格分隔;
若使用gets函数,应为gets(str1); gets(str2); 字符串之间用回车符作分隔。在通常情况下,接受短字符用scanf函数,接受长字符用gets函数。getchar函数每次只接受一个字符,经常c=getchar()这样来使用。在C++中读入字符串通常使用cin.getline函数,可以接受用户的输入的字符,直到已达指定个数,或者用户输入了特定的字符。它的函数声明形式(函数原型)如下:
istream& getline(char line[], int size, char endchar = '\n');
char line[]: 就是一个字符数组,用户输入的内容将存入在该数组内。
int size : 最多接受几个字符,用户超过size的输入都将不被接受。
char endchar :当用户输入endchar指定的字符时,自动结束,默认是回车符。
(六)第一类输出:一个Input Block对应一个Output Block,Output Block之间没有空行。
Example5:
Sample Input:
1 5
10 20
Sample Output:
6
30
这里我们主要关心输出格式,同样是计算a+b的和,要求没输出两个数的和就要求输出一个换行。本类的输出方式如下:
C语法:
{
//计算语句
printf("%d\n",ans);
}
C++语法:
{
//计算语句
cout << ans << endl;
}
(七)第二类输出:一个Input Block对应一个Output Block,每个Output Block之后都有空行。
Example6:
Sample Input:
1 5
10 20
Sample Output:
6
(这里有空行)
30
(这里有空行)
这里的输出方式很上面一类的输出方式唯一不同的是,每一组数据结果输出后跟了两次换行,第一换行是将输出光标移到下一行的输出其实位置,第二次换行是为了增加一个空行,让输出光标的位置再次移动到下一行的其实位置。
本类的输出方式如下:
C语法:
{
//计算语句
printf("%d\n\n",ans);
}
C++语法:
{
//计算语句
cout << ans << endl << endl;
}
(八)第三类输出:一个Input Block对应一个Output Block,每个Output Block之后都有空行。注意:这里是跟第二类输出有所区别,第二类四每一个输出块后紧跟一个空行,这里是输出块之间有空行,意思就是说最后一个输出块之后是没有空行的。当然,在实际的编程中,除非明确知道有多少个输入块,否则最后一个空行确实不太好控制,当然要是算法题目中没有明确告知有多少个输入块,仍旧采用本类输出方式,一般对最后一个多余的空行是不做格式检查的。
Example7:
Sample Input:
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
Sample Output:
10
(这里有空行)
15
(这里有空行)
6(这里没有空行)
本类的输出方式如下:
C语法:
scanf(“%d”,&count);
for (k=0;k<count;k++)
{
while (…)
{
printf(" %d\n",result);
}
if (k!=count-1) printf("\n");
}
C++语法:
类似,输出语句换一下即可。