版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://cloud.tencent.com/developer/article/1535043
A + B Problem
Time Limit: 2000 msMemory Limit: 65536 KB
Calculate a + b
The input will consist of a series of(原谅我英文不好,没看到这个) pairs of integers a and b,separated by a space, one pair of integers per line.
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.
1 5
6
Use + operator
#include<iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{printf("%d\n",a + b);
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
string a,b;
int c=a.length()>b.length()? b.length():a.length();
int d=a.length()<b.length()? b.length():a.length();
int count=0;
string s;
for(int i=0;i<d;i++){
if(a.length()>i){
count+=a[a.length()-i-1]-'0';
}if(b.length()>i){
count+=b[b.length()-i-1]-'0';
}
char o=(count%10+'0');
s=o+s;
count=count/10;
if(count>0){
s+=(count%10+'0');
}
cout<<s<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b;
while(scanf("%d %d",&a,&b))
{printf("%d\n",a + b);
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{printf("%d\n",a + b);
}
return 0;
}
scanf函数返回成功读入的数据项数,读入数据时遇到了“文件结束”则返回EOF。 因为浙大的oj是文件读入,那就判断EOF呗, 开始想的是读入错误应该是返回0,就跳出while了,但是仍然会卡在while (其实自己不用文件读写的时候都没有跳出while循环....)
#include<iostream>
using namespace std;
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)!=EOF)
{printf("%d\n",a + b);
}
return 0;
}