题目描述
你的任务是计算a+b。
输入
第一行是一个整数N,表示后面会有N行a和b,通过空格隔开。
输出
对于输入的每对a和b,你需要在相应的行输出a、b的和。
如第二对a和b,对应的和也输出在第二行。
样例输入
2
1 5
10 20
样例输出
6
30
源代码
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
int a,b;
int n;
scanf("%d",&n);
for(int i = 0; i < n; i ++){
scanf("%d %d",&a,&b);
printf("%d\n",a+b);
}
}
总结
与上一题的区别在于,循环的次数就是为n。不用再去处心积虑的去判断循环的次数了。
补充
在前面的一个问题中我有的疑问,就是将数据先输入完成后再使其输出值的这个疑问在网上也有人问,经过思考后我认为可以用结构体来解决。写了如下的代码,并在oj上测试正确
#include <iostream>
#include <stdio.h>
using namespace std;
struct store{
int x;
int y;
};
int main(){
int a,b;
int n;
int sum;
scanf("%d",&n);
struct store stor[n];
for(int i = 0;i < n; i ++){
scanf("%d %d",&a,&b);
stor[i].x = a;
stor[i].y = b;
}
for(int i = 0; i < n; i++){
sum = stor[i].x + stor[i].y;
printf("%d\n",sum);
}
}