输入_第一类
- 输入不说明有多少个Input Block,以EOF为结束标志。
例题(HDOJ1089)
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.
while(scanf("%d %d",&a, &b) != EOF)//1.Scanf函数返回值就是读出的变量个数,如:scanf( “%d %d”, &a, &b );
{ 如果只有一个整数输入,返回值是1,如果有两个整数输入,返回值是2,如果一个都没有,则返回值是-1
.... 2.EOF是一个预定义的常量,等于-1
}
while( cin >> a >> b )
{
....
}
Java语法:
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
a=scan.nextInt();
b=scan.nextInt();
....
}
- 输入一开始就会说有N个Input Block,下面接着是N个Input Block。
例题(HDOJ1090)
本类输入解决方法
scanf("%d",&n) ;
for( i=0 ; i<n ; i++ )
{
....
}
cin >> n;
for( i=0 ; i<n ; i++ )
{
....
}
java语法:
n=scan.nextInt();
for( i=0 ; i<n ; i++ )
{
....
}
输入_第三类
- 输入不说明有多少个Input Block,但以某个特殊输入为结束标志。
例题(HDOJ1091)
本类输入解决方法
while(scanf("%d",&n) && n!=0 )
{
....
}
while( cin >> n && n != 0 )
{
....
}
Java语法:
while( n=scan.nextInt&&n!=0 )
{
....
}
输入_第五类
- 输入是一整行的字符串的
例题(HDOJ1048)
char buf[20];
gets(buf);
C++语法:
如果用string buf;来保存:
getline( cin , buf );
如果用char buf[ 255 ]; 来保存:
cin.getline( buf, 255 );//
istream& getline(char line[], int size, char endchar = '\n');