A + B Problem
Problem Description
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
include <iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}
Wrong Answer
原因:理解错题意,本题要求把每一行的两个数字加起来,然后打印出来,直到文件末尾。我理解为将两个数加起来,输出它们的和。
include <iostream>
using namespace std;
int main()
{
int a,b;
while(1)
{
cin>>a>>b;
cout<<a+b<<endl;
}
return 0;
}
Time Limit Exceeded
while(1)始终是真,因此会一直要求输入,所以会超时.正确做法把输入放在判断出,那么当你输入到最后也就是不想再输入的时候它会停止的
include <iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
cout<<a+b<<endl;
}
return 0;
}