emmm刚刚看到了有人评论了我以前写的一些文章,深受鼓舞。打算把最近要学的整理到这里面来
废话不多说,直接步入正题
杭电oj1000
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嘛,但是在我用刚学的cpp写的时候就出问题了
代码如下:
#include <iostream>
using namespace std;
int main(){
int a,b;
while ((cin>>a && cin>>b )!=EOF)
cout << a+b << endl;
return 0;
}
然后提交,错误
还是神仙错误Time Limit Exceeded
然后不服的hwq在网上看了很多资料,发现了这样一段话
因为c的输入数出效率高得多,程序运行时间会快很多。甚至有些题用cin,cout解题会tle,换成scanf和printf就过了。看过大神用随机数测试过scanf和cin的效率,最大可能相差十倍。
所以最终还是使用了这样一段代码(用C写的)
#include <stdio.h>
int main()
{
int a, b;
while (scanf("%d%d", &a, &b) != EOF)
printf("%d\n", a + b);
return 0;
}