1. 程式人生 > >ACM第一期練習第四小題:A + B Problem

ACM第一期練習第四小題:A + B Problem

A+B Problem
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
Sample Output
2
問題簡述:
第一行輸入兩個整型數字並以空格分開,第二行輸出A+B的結果,並且要可以輸入多組資料。
問題分析:
為了實現可以輸入多組資料即程式不能結束,考慮利用while搭配 EOF 使用。
Virtual Judge通過的程式碼如下:

#include <iostream>
using namespace std;
int main()
{
	int a, b;
	while (scanf_s("%d %d", &a, &b) != EOF)//能夠讓while 下面的語句一直執行不會讓程式結束
	{
		printf("%d\n", a + b);
	}
	return 0;
}