ZOJ 1001 A + B Problem
A + B Problem
Time Limit: 2000 ms Memory Limit: 65536 KB
Calculate a + b
Input
The input will consist of a series of pairs 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
1 5
Sample Output
6
Hint
Use + operator
这是一道例题,提交区上面甚至已经给出了Sample Program Here
,这道题的意义在于熟悉ACM题目的输入
在以往的练习中,数据都是直接复制粘贴到console,程序读取运行,而ACM题目采用的是文件输入,需要自行判断EOF
标志
C++版本
#include <iostream>
using namespace std;
int main()
{
int a,b;
while(cin >> a >> b)
cout << a+b << endl;
}
C版本
#include <stdio.h>
int main()
{
int a,b;
while(scanf("%d %d",&a, &b) != EOF)
printf("%d\n",a+b);
return 0;
}
控制台
在本地调试的时候,在键盘上按下ctrl + z
,可以手动置EOF标志
文件
使用文件进行输入更为简单快捷
VS2019在源文件里面创建输入文件a.txt
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream cin("a.txt");
int a, b;
while (cin >> a >> b) {
cout << a + b << endl;
}
return 0;
}