#include <iostream>
using namespace std;
int main() {
// write your code here......
int a, b;
while(cin>>a>>b && a>0 && b>0){
if(a>b){
cout <<a+b<<" "<<a-b<<" "<<a*b<<" "<<a/b<<" "<<a%b;
}else{
cout <<a+b<<" "<<b-a<<" "<<a*b<<" "<<b/a<<" "<<b%a;
}
}
return 0;
}
可优化的写法:使用三目运算符和数组循环输出
#include <iostream>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b && a > 0 && b > 0) {
// 1. 确定大小关系,计算差值、商和余数
int larger = (a > b) ? a : b;
int smaller = (a > b) ? b : a;
// 2. 将所有结果存入数组
int results[] = {a + b, larger - smaller, a * b, larger / smaller, larger % smaller};
// 3. 使用循环统一输出
for (int i = 0; i < 5; ++i) {
cout << results[i];
if (i < 4) cout << " "; // 最后一个数后面不跟空格
}
cout << endl; // 记得换行
}
return 0;
}
}

京公网安备 11010502036488号