2. 编写一个程序,用来求2个或3个整数中的最大数。
时间: 2023-06-05 16:04:46 浏览: 50
好的,这个问题非常简单。以下是一个示例代码实现:
```python
# 求 2 个整数的最大数
def get_max(a, b):
return a if a > b else b
# 求 3 个整数的最大数
def get_max_three(a, b, c):
return get_max(get_max(a, b), c)
# 示例
print(get_max(3, 5)) # 输出 5
print(get_max_three(1, 9, 5)) # 输出 9
```
希望这个示例可以帮到您!
相关问题
编写一个程序,用来求2个或3个整数中的最大输,,用函数重载实现
#include<iostream>
using namespace std;
int max(int a, int b) {
return (a > b) ? a : b;
}
int max(int a, int b, int c) {
return max(max(a, b), c);
}
int main() {
int a, b, c;
cout << "请输入2个或3个整数,用空格隔开:" << endl;
cin >> a >> b;
if (cin.peek() == ' ') {
cin.ignore();
cin >> c;
cout << "最大数为:" << max(a, b, c) << endl;
}
else {
cout << "最大数为:" << max(a, b) << endl;
}
return 0;
}
编写一个程序,用来求2个或3个整数中的最大输,,用函数模板实现
```C++
#include <iostream>
using namespace std;
template<typename T>
T max(T a, T b) {
return a > b ? a : b;
}
template<typename T>
T max(T a, T b, T c) {
return max(max(a, b), c);
}
int main() {
int a = 10, b = 20, c = 15;
cout << "max of two integers: " << max(a, b) << endl;
cout << "max of three integers: " << max(a, b, c) << endl;
double d = 3.14, e = 2.718, f = 2.0;
cout << "max of two doubles: " << max(d, e) << endl;
cout << "max of three doubles: " << max(d, e, f) << endl;
return 0;
}
```