有3个整数a、b、c由键盘输入,输出其中最大的数。
时间: 2024-02-24 10:39:49 浏览: 78
算法1:分支结构
1. 读入三个整数 a、b、c。
2. 假设 a 是最大值,比较 b 和 c 是否大于 a,如果是则更新最大值为 b 或 c。
3. 输出最大值。
时间复杂度:O(1)
C++ 代码:
#include <iostream>
using namespace std;
int main() {
int a, b, c, max_num;
cin >> a >> b >> c;
max_num = a;
if (b > max_num) {
max_num = b;
}
if (c > max_num) {
max_num = c;
}
cout << max_num << endl;
return 0;
}
算法2:库函数
1. 读入三个整数 a、b、c。
2. 使用 C++ 标准库中的 max 函数比较三个数的大小。
3. 输出最大值。
时间复杂度:O(1)
C++ 代码:
#include <iostream>
#include <algorithm> // 包含 max 函数
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
int max_num = max(max(a, b), c);
cout << max_num << endl;
return 0;
}
相关问题
有3个整数a b c,由键盘输入,输出其中最大的数。
C知道:您好!以下是您需要的答案:
可以使用 if-else 语句来比较三个数的大小,具体代码如下:
#include <stdio.h>
int main() {
int a, b, c, max;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
max = a;
} else {
max = b;
}
if (c > max) {
max = c;
}
printf("最大的数是:%d\n", max);
return 0;
}
如果您有其他问题,欢迎随时提出。
有3个整数a,b,c由键盘输入,输出其中最大的数
以下是Python代码实现:
```
a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
c = int(input("请输入第三个整数:"))
if a > b and a > c:
print("最大的数是:", a)
elif b > a and b > c:
print("最大的数是:", b)
else:
print("最大的数是:", c)
```
以下是示例输出:
```
请输入第一个整数:5
请输入第二个整数:8
请输入第三个整数:3
最大的数是: 8
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)