输入3个整数按由大到小顺序switch出
时间: 2023-06-01 20:02:26 浏览: 81
对于机器人来说,switch语句并不适用于输入3个整数并按由大到小顺序排列的情况。以下是一种可以实现此功能的Python代码:
a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
c = int(input("请输入第三个整数:"))
if a >= b and a >= c:
if b >= c:
print("由大到小排列为:", a, b, c)
else:
print("由大到小排列为:", a, c, b)
elif b >= a and b >= c:
if a >= c:
print("由大到小排列为:", b, a, c)
else:
print("由大到小排列为:", b, c, a)
else:
if a >= b:
print("由大到小排列为:", c, a, b)
else:
print("由大到小排列为:", c, b, a)
相关问题
用switch方法写出输入a、b、c三个整数,将他们从小到大的顺序输出
好的,我可以回答这个问题。请看下面的代码:
```
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
switch (a < b) {
case 1:
switch (b < c) {
case 1:
printf("%d %d %d\n", a, b, c);
break;
case 0:
switch (a < c) {
case 1:
printf("%d %d %d\n", a, c, b);
break;
case 0:
printf("%d %d %d\n", c, a, b);
break;
}
break;
}
break;
case 0:
switch (a < c) {
case 1:
printf("%d %d %d\n", b, a, c);
break;
case 0:
switch (b < c) {
case 1:
printf("%d %d %d\n", b, c, a);
break;
case 0:
printf("%d %d %d\n", c, b, a);
break;
}
break;
}
break;
}
return 0;
}
```
这个程序可以读入三个整数,然后用 switch 方法将它们从小到大的顺序输出。
编写c语言程序,由键盘任意输入三个数据,按由小到大的顺序输出
```
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
printf("三个数由小到大的顺序为:%d %d %d\n", a, b, c);
return 0;
}
```
注意事项:
- 本程序使用了条件语句if,if else或switch case语句也可以实现。
- 本程序用了较多的变量交换,也可以使用数组或指针操作来实现。
- 输入函数scanf要添加"%d"格式控制符,读取整数类型数据。
相关推荐
![](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)