首先我选择的是c语言的解法,开始是输入如下的答案:
#include "stdio.h"
int main(void)
{
long long a,b;
scanf("%d", &a);
scanf("%d", &b);
printf("%d\n", a+b);
}
该程序只能输入一次数据,而杭电acm1000题问题描述是:
Each line will contain two integers
A and
B. Process to end of file.
意思是每一行包含A和B这两个int型的整数,
Process to end of file 就是处理到文件的结束。
把每一行的两个数字加起来,然后打印出来,直到文件末尾。
所以代表可以输入多组数据,直到你的输入EOF为止。
正确的程序应该是:
#include "stdio.h"
int main(void)
{
int a, b;
while (scanf("%d %d", &a, &b) != EOF)
printf("%d\n", a + b);
}
对于
while (scanf("%d %d", &a, &b) != EOF)的解释如下;
1、eof在c中定义为-1,vs2013的原话为
#define EOF -1;
2、只要scanf()读取一个整数,就会返回1.
3、所以它等价于while (scanf("%d %d", &a, &b) != -1),即scanf仍在持续接受键盘输入区内的整数数据进入到标准输入区。
windows中ctrl+Z 会产生 一个 EOF 码,拍入 键盘缓冲区,因为没有单个EOF 键,只能用组合键拍入。Linux的又不一样的
。