题目-005:题目:输入三个整数x,y,z,请把这三个数由小到大输出
- 分析:分析比较,先用x与y比较排序,然后再和后面的一次比较,确定x为最小;然后再后面确定y为最小,输出结果;
- Python版本:Python 3.6.5
代码1:基本代码,当然还是最经典的冒泡排序
#! usr/bin/python
#! -*- coding: utf-8 -*-
x = int(input("please input the number x:"))
y = int(input("please input the number y:"))
z = int(input("please input the number z:"))
data = [x,y,z]
print("the number you input is:",data)
for i in range(0,3):
for j in range(i+1,3):
if data[i] > data[j]:
data[i],data[j] = data[j],data[i]
print("the number Sort from small to large is:",data)
please input the number x:9
please input the number y:3
please input the number z:-5
the number you input is: [9, 3, -5]
the number Sort from small to large is: [-5, 3, 9]
代码2:都说Python是很简洁的语言,当然也不需要那么多代码,一个sort函数就搞定了
#! usr/bin/python
#! -*- coding: utf-8 -*-
x = int(input("please input the number x:"))
y = int(input("please input the number y:"))
z = int(input("please input the number z:"))
data = [x,y,z]
print("the number you input is:",data)
data.sort()
print("the number Sort from small to large is:",data)
please input the number x:6
please input the number y:8
please input the number z:9
the number you input is: [6, 8, 9]
the number Sort from small to large is: [6, 8, 9]
代码3:例行拓展,不限定输入个数,封装后循环运行
#! usr/bin/python
#! -*- coding: utf-8 -*-
def SortNumber():
data = []
while True:
try:
input_A = int(input("please input the number:"))
data +=[input_A]
except:
break
print("the number you input is:",data)
data.sort()
print("the number Sort from small to large is:",data)
print("End -*- End "*5)
while True:
SortNumber()
please input the number:9
please input the number:6
please input the number:3
please input the number:-4
please input the number:8
please input the number:100
please input the number:-99
please input the number:0
please input the number:
the number you input is: [9, 6, 3, -4, 8, 100, -99, 0]
the number Sort from small to large is: [-99, -4, 0, 3, 6, 8, 9, 100]
End -*- End End -*- End End -*- End End -*- End End -*- End