用c++写一个有三个整数a.b.c由键盘输入,输出其中最大的数
时间: 2023-05-19 10:00:54 浏览: 530
以下是用 C 语言编写的程序,可以实现输入三个整数 a、b、c,然后输出其中最大的数:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
printf("最大的数是:%d\n", max);
return 0;
}
```
注意,这个程序只能处理整数,如果输入的是小数或其他类型的数据,程序可能会出错。
相关问题
有3个整数a、b、c由键盘输入,输出其中最大的数。
算法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;
}
c++写一个程序::从键盘输入3个整数,将这3个数由大到小进行排序后输出。
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
if (a >= b && a >= c) {
if (b >= c) {
printf("%d %d %d", a, b, c);
} else {
printf("%d %d %d", a, c, b);
}
} else if (b >= a && b >= c) {
if (a >= c) {
printf("%d %d %d", b, a, c);
} else {
printf("%d %d %d", b, c, a);
}
} else {
if (a >= b) {
printf("%d %d %d", c, a, b);
} else {
printf("%d %d %d", c, b, a);
}
}
return 0;
}
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)