输入3个整数,按从小到大的顺序输出。试编写相应程序
时间: 2023-05-25 08:03:03 浏览: 54
a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
c = int(input("请输入第三个整数:"))
if a > b:
a, b = b, a
if a > c:
a, c = c, a
if b > c:
b, c = c, b
print("从小到大排序后的数字依次为:", a, b, c)
相关问题
比较大小:输入3 个整数n1、n2 和n3,将这3 个数按从小到大的顺序输出。试编写相应程序。用C语言编写
以下是用C语言编写的程序:
```c
#include <stdio.h>
int main() {
int n1, n2, n3;
printf("请输入三个整数n1、n2、n3:");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 <= n2 && n1 <= n3) {
printf("%d ", n1);
if (n2 <= n3) {
printf("%d %d\n", n2, n3);
} else {
printf("%d %d\n", n3, n2);
}
} else if (n2 <= n1 && n2 <= n3) {
printf("%d ", n2);
if (n1 <= n3) {
printf("%d %d\n", n1, n3);
} else {
printf("%d %d\n", n3, n1);
}
} else {
printf("%d ", n3);
if (n1 <= n2) {
printf("%d %d\n", n1, n2);
} else {
printf("%d %d\n", n2, n1);
}
}
return 0;
}
```
程序中使用了条件语句if和else if来判断三个数的大小关系,并按照从小到大的顺序输出。
写一段代码实现一下功能:输入一个整数参数M,如果输入参数M小于等于1或者大于等于100,输出“ERROR!”;否则按照原先的编号从小到大的顺序,以英文逗号分割输出编号字符串
下面是一个示例代码,实现了你描述的功能:
```python
def generate_number_string(M):
if M <= 1 or M >= 100:
return "ERROR!"
else:
number_list = [str(i) for i in range(1, M + 1)]
return ",".join(number_list)
# 测试
M = int(input("请输入整数M:"))
result = generate_number_string(M)
print(result)
```
这段代码定义了一个函数 `generate_number_string`,接受一个整数参数 M。如果 M 小于等于 1 或大于等于 100,则返回 "ERROR!";否则,生成从 1 到 M 的编号字符串,并以英文逗号分割。最后进行测试,通过输入一个整数 M,即可输出相应的结果。
注意:这段代码是用 Python 编写的,你可以在本地的 Python 环境中运行。
相关推荐
![png](https://img-home.csdnimg.cn/images/20210720083516.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)