因为电脑原因,只做了一题,这里就写一个了
A+B for Input-Output Practice (I)
Time Limit:1000MS Memory Limit:10000K(题目的运行时间和内存限制)
Total Submit:111 Accepted:36 (当前提交总次数 正确次数)
Description (题目描述)
Your task is to Calculate a + b.
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
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
10 20
Sample Output (样例输出)
6
30
这是一个求两数之和的题目,输入用空格分开的两个数a b,输出a+b的结果。编写代码时需要注意的是,本题的输入数据不是只有一对数据,而是多对数据,每对数据占一行。因为没有指出共有多少对输入数据,于是我们可以编写如下代码:
- #include <stdio.h>
- int main() //把main函数定义成int类型
- {
- int a,b;
- while(scanf("%d %d",&a, &b) != EOF) // 输入文件结束时,scanf函数返回值为EOF
- printf("%d\n",a+b); // 直到没有数据输入则退出while循环
- return 0;
- }
- //或者C++语言
- #include <iostream>
- using namespace std;
- int main()
- {
- int a,b;
- while(cin >> a >> b)
- cout << a+b << endl;
- return 0;
阅读(2145) | 评论(0) | 转发(0) |