用if语句把3个数的从大到小排列
时间: 2023-05-11 10:04:24 浏览: 29
可以这样写:
a = 10
b = 5
c = 20
if a > b:
if a > c:
if b > c:
print(a, b, c)
else:
print(a, c, b)
else:
print(c, a, b)
else:
if b > c:
if a > c:
print(b, a, c)
else:
print(b, c, a)
else:
print(c, b, a)
相关问题
用if语句完成三个数从大到小的输出
```python
a = int(input("请输入第一个数:"))
b = int(input("请输入第二个数:"))
c = int(input("请输入第三个数:"))
if a >= b:
if b >= c:
print(a, b, c)
elif a >= c:
print(a, c, b)
else:
print(c, a, b)
else:
if a >= c:
print(b, a, c)
elif b >= c:
print(b, c, a)
else:
print(c, b, a)
```
解释:首先通过 `input()` 函数获取三个数的值,然后使用嵌套的 `if` 语句进行比较,最终输出三个数从大到小的排列。
用c语言if双分支从大到小排列三个数字大小
可以使用以下代码实现:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个数字:\n");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && b >= c) {
printf("%d >= %d >= %d\n", a, b, c);
} else if (a >= c && c >= b) {
printf("%d >= %d >= %d\n", a, c, b);
} else if (b >= a && a >= c) {
printf("%d >= %d >= %d\n", b, a, c);
} else if (b >= c && c >= a) {
printf("%d >= %d >= %d\n", b, c, a);
} else if (c >= a && a >= b) {
printf("%d >= %d >= %d\n", c, a, b);
} else {
printf("%d >= %d >= %d\n", c, b, a);
}
return 0;
}
```
这段代码可以让用户输入三个数字,然后使用 if 双分支语句从大到小排列这三个数字的大小,并输出结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)