#include <iostream> using namespace std; void f(double A, double B, double C) { if (A > B) { if (B > C) cout << A << ">" << B << ">" << C << endl; } if (B > C) { if (C > A) cout << B << ">" << C << ">" << A << endl; } if (C > A) { if (A > B) cout << C << ">" << A << ">" << B << endl; } } int main() { double A, B, C; cout << "输入A,B,C:" << endl; cin >> A >> B >> C; f(A, B, C); return 0; }增加一个取最大值MAX
时间: 2024-02-16 09:27:50 浏览: 54
可以在函数中添加一个变量max来存储当前最大值,每次比较更新这个变量即可。修改后的代码如下:
```c++
#include <iostream>
using namespace std;
void f(double A, double B, double C) {
double max = A;
if (B > max) {
max = B;
}
if (C > max) {
max = C;
}
if (max == A) {
if (B > C) {
cout << A << ">" << B << ">" << C << endl;
}
else {
cout << A << ">" << C << ">" << B << endl;
}
}
else if (max == B) {
if (A > C) {
cout << B << ">" << A << ">" << C << endl;
}
else {
cout << B << ">" << C << ">" << A << endl;
}
}
else {
if (A > B) {
cout << C << ">" << A << ">" << B << endl;
}
else {
cout << C << ">" << B << ">" << A << endl;
}
}
}
int main() {
double A, B, C;
cout << "输入A,B,C:" << endl;
cin >> A >> B >> C;
f(A, B, C);
return 0;
}
```
在这个修改后的代码中,我们添加了一个变量max来存储当前最大值。首先,我们将A赋值给max作为初始最大值。然后,分别比较B和C与max的大小,如果比max大,就更新max的值。最后,根据max的值和A、B、C的大小关系输出结果。
阅读全文