0
点赞
收藏
分享

微信扫一扫

每日一练2——排序问题(Python&C语言实现)


目录

​​1 案例描述​​

​​2 Python实现​​

​​2.1 方法一(官方)​​

​​2.2 方法二​​

​​3 C语言实现 ​​

1 案例描述

输入三个整数x,y,z,请把这三个数由小到大输出。

2 Python实现

2.1 方法一(官方)

L = []
for i in range(3):
x = int(input('integer:\n'))
L.append(x)
L.sort()
print(L)

#==========结果=====================

integer:
23
integer:
34
integer:
9
[9, 23, 34]

Process finished with exit code 0

简洁明了,痛快、痛快! 

2.2 方法二

new_list = input("请输入三个整数:")
a_list = new_list.split(",", 3)
a_list = list(map(int, a_list))
while len(a_list) > 0:
number = max(a_list)
print(number)
a_list.remove(number)

print('#=======过程解析==========#')
print(type(new_list))
print(type(a_list))

请输入三个整数:23,34,9
34
23
9
#=======过程解析==========#
<class 'str'>
<class 'list'>

Process finished with exit code 0

重要知识点: (1)指定分隔符对字符串进行切片split(str="", num=string.count(str))​​​​前面填自己选择的分隔符,后面填需要分割成多少个的数值​​​​(2)map()内置函数用处比较多,这里我暂时只用了它的一种方法:将str类型转成了int类型​​​​(3)列表的max()方法,找到列表里的最大的数字​​​​(4)列表的remove()方法,移除指定的元素​

3 C语言实现 

#include<stdio.h>
void main()
{
int x,y,z,t;
scanf("%d,%d,%d",&x,&y,&z);
if(x>y)
{
t=x;
x=y;
y=t;
}

if(x>z)
{
t=x;
x=z;
z=t;
}

if(y>z)
{
t=y;
y=z;
z=t;
}
printf("从小到大依次为: %d %d %d\n",x,y,z);
}

每日一练2——排序问题(Python&C语言实现)_开发语言

举报

相关推荐

0 条评论