用c++输入4个整数,要求用交换法输出4个数中的最大值。
时间: 2024-02-18 21:05:49 浏览: 40
好的,下面是一个用交换法输出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个数中的最大值,最后再用交换法输出最大值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)