1000: A+B 输入输出练习I
题目描述
你的任务是计算a+b。这是为了acm初学者专门设计的题目。你肯定发现还有其他题目跟这道题的标题类似,这些问题也都是专门为初学者提供的。
输入
输入包含一系列的a和b对,通过空格隔开。一对a和b占一行。
输出
对于输入的每对a和b,你需要依次输出a、b的和。
如对于输入中的第二对a和b,在输出中它们的和应该也在第二行。
样例输入
1 5
10 20
样例输出
6
30
来源/分类
分析:
我们在这用一个表格体现:
x | y | 结果 |
1 | 1 | 1+1=2 |
7 | 3 | 7+3=10 |
10 | 11 | 10+11=21 |
134 | 462 | 134+462=596 |
678924 | 5729109 | 678924+5729109=6408033 |
…… | …… | x+y |
代码:
#include<bits/stdc++.h>//个人习惯用万能头
using namespace std;
int main(){
int a,b;
while(cin>>a>>b){
cout<<a+b<<endl;
}
return 0;
}
错误代码展示:
1:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<a+b<<endl<<c+d<<endl;
}
错误原因:只按照样例写了2组。
2:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
while(1){
cin>>a>>b;
cout<<a+b<<endl;
}
}
错误原因:输入输出无限进行。
各位小伙伴,有没有也中招了呢?快参与投票吧!!!