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
题目大意
实现一个a+b的程序。
分析
这是杭电Online Exercise的第一题。
这么简单的题目就是为了让我入门的吧。
于是洋洋洒洒的写下了几行代码,直接提交。
# include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
结果第一次就Wrong Answer。
第一题就出乎我的意料,这是要劝退吗?
然后我再看题目。
Process to end of file.
好吧。
其实中应该有许多测试数据,要用一个循环来让他一直接受测试,直到EOF。
下面是c++的代码
# include <iostream>
using namespace std;
int main()
{
int a,b,sum;
while (cin>>a>>b) //EOF判断
{
sum=a+b;
cout<<sum<<endl;
}
return 0;
}
不过C的可能更加直接
# include <stdio.h>
int main()
{
int a,b,sum;
while (scanf("%d%d",&a,&b) != EOF)
{
sum=a+b;
printf("%d\n",sum);
}
return 0;
}
总结
第一次开始写博客,就从这里开始吧。很简单的题目,虽然Wrong了,但是也算是一次有深刻教训的开始吧。
关键还是要坚持!
答应我,点个赞呗!