用c++求2个整数,3个整数的最大值,用同名函数实现
时间: 2023-12-17 14:03:18 浏览: 26
以下是用同名函数实现2个整数和3个整数的最大值的C++代码:
```c++
#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 x = 5, y = 10;
cout << "Max of " << x << " and " << y << " is " << max(x, y) << endl;
int p = 15, q = 20, r = 25;
cout << "Max of " << p << ", " << q << " and " << r << " is " << max(p, q, r) << endl;
return 0;
}
```
输出:
```
Max of 5 and 10 is 10
Max of 15, 20 and 25 is 25
```
相关问题
求2个整数,3个整数的最大值,用同名函数实现,用c++
以下是求2个整数和3个整数的最大值的C语言代码:
```c
#include <stdio.h>
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 x = 10, y = 20, z = 30;
printf("Max of %d and %d is %d\n", x, y, max(x, y));
printf("Max of %d, %d and %d is %d\n", x, y, z, max(x, y, z));
return 0;
}
```
输出:
```
Max of 10 and 20 is 20
Max of 10, 20 and 30 is 30
```
用c++实现 ,求2个整数,3个整数的最大值,用同名函数实现
#include <stdio.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
int max(int a, int b, int c) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return max;
}
int main() {
int a, b, c;
printf("请输入两个整数:");
scanf("%d %d", &a, &b);
printf("%d和%d中的最大值为:%d\n", a, b, max(a, b));
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
printf("%d、%d和%d中的最大值为:%d\n", a, b, c, max(a, b, c));
return 0;
}