用c++输入4个整数,要求用交换法输出4个数中的最大值。
时间: 2024-02-18 16:05:49 浏览: 65
好的,下面是一个用交换法输出4个数中的最大值的C++程序:
```c++
#include <iostream>
using namespace std;
int main() {
int a, b, c, d, max;
cout << "请输入4个整数:" << endl;
cin >> a >> b >> c >> d;
max = a;
if (b > max) max = b;
if (c > max) max = c;
if (d > max) max = d;
cout << "最大值为:" << max << endl;
// 使用交换法输出最大值
if (max == a) {
cout << b << " " << c << " " << d << " " << a << endl;
} else if (max == b) {
cout << a << " " << c << " " << d << " " << b << endl;
} else if (max == c) {
cout << a << " " << b << " " << d << " " << c << endl;
} else {
cout << a << " " << b << " " << c << " " << d << endl;
}
return 0;
}
```
我们先用`cin`语句输入4个整数,然后用`if`语句找出4个数中的最大值,最后再用交换法输出最大值。
阅读全文