目录
- C++ 输入三个数找出最大值的多种方法
-
- 方法 1:使用 if-else 语句
- 方法 2:嵌套 if 语句
- 方法 3:使用条件运算符 (三元运算符)
- 方法 4:使用 std::max 函数
- 方法 5:使用数组和循环
- 方法 6:使用函数模板
- 方法 7:使用指针
C++ 输入三个数找出最大值的多种方法
在 C++ 中,有多种方法可以找出三个数中的最大值。以下是几种常见的实现方式:
方法 1:使用 if-else 语句
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "请输入三个整数: ";
cin >> a >> b >> c;
int max;
if (a >= b && a >= c) {
max = a;
} else if (b >= a && b >= c) {
max = b;
} else {
max = c;
}
cout << "最大值是: " << max << endl;
return 0;
}
方法 2:嵌套 if 语句
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "请输入三个整数: ";
cin >> a >> b >> c<