Python 练习实例5
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
程序源代码:
实例(Python 2.0+)
实例(Python 3.0+)
以上实例输出结果为:
integer: 8 integer: 5 integer: 6 [5, 6, 8]
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
程序源代码:
以上实例输出结果为:
integer: 8 integer: 5 integer: 6 [5, 6, 8]
somejune
502***[email protected]
其他参考解法:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
x = int(raw_input("x:"))
y = int(raw_input("y:"))
z = int(raw_input("z:"))
a = {"x":x,"y":y,"z":z}
print '--------分割线--------'
for w in sorted(a, key=a.get):
print w, a[w]
输出结果:
x:5 y:2 z:8 --------分割线-------- y 2 x 5 z 8
somejune
502***[email protected]
lv二呆
may***[email protected]
参考解法:
#!/usr/bin/python # -*- coding: UTF-8 -*- a=[1,3,5,2,4,5,7] n=len(a) for i in range(0,n): for j in range(i,n) : if (a[i] >= a[j] ): tmp =a[i] a[i]=a[j] a[j]=tmp print a
lv二呆
may***[email protected]
流年细雨
758***[email protected]
参考解法:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
x= raw_input("int1:")
y= raw_input("int2:")
Max = max(x,y)
Min = min(x,y)
z= raw_input("int3:")
if z > Max :
print Min,Max,z
elif z < Min :
print z,Min,Max
else :
print Min,z,Max
流年细雨
758***[email protected]
Alen
554***[email protected]
Python3.x 版本下使用利用冒泡排序方法:
#!/usr/bin/env python3
# 利用冒泡排序方法
def Sort(list):
n = len(list)
for i in range(1, n):
# 一次次的将最大的学出来
for j in range(1, n - i + 1):
if list[j - 1] > list[j]:
list[j - 1], list[j] = list[j], list[j - 1]
# 打印排序过程
print(list)
for i in range(0, n):
print(list[i])
# 读入数据
def inputData():
list_first = []
while True:
a = input("please input num:".strip())
if len(a) == 0:
return list_first
else:
list_first.append(int(a))
if __name__ == '__main__':
lt = inputData()
print(lt)
Sort(lt)
Alen
554***[email protected]
Atom
tum***@126.com
使用 列表 sort=,可接受参数 reverse,默认为布尔值 false,按升序排序,设置为 true 则按降序排序
#!/usr/bin/python
# -*- coding: UTF-8 -*-
x = int(input('x='))
y = int(input('y='))
z = int(input('z='))
num = [x, y, z]
num.sort() # 对列表进行升序排序
print '这三个数由小到大的顺序为:',num
rnum = [x, y, z] # 对列表进行降序排序
rnum.sort(reverse=True)
print '这三个数由大到小的顺序为:',rnum
Atom
tum***@126.com
初学者
646***[email protected]
参考方法:
#!/usr/bin/env python
#coding:utf-8
D=raw_input("请输入三个数字,格式如 'a,b,c':")
li=D.split(",")
li2=[int(i) for i in li]
li2.sort()
print li2
初学者
646***[email protected]
storm
450***[email protected]
对输入类型进行了控制,如果输入错误,就提示用户,让用户再次输入,直到正确输入整数为止。
#!/usr/bin/env python
#coding:utf-8
#输入三个整数x,y,z,请把这三个数由小到大输出。
while 1:
try:
x = int(input("plz input x: "))
y = int(input("plz input y: "))
z = int(input("plz input z: "))
list1 = [x, y, z]
print(sorted(list1))
break
except:
print("请输入整数")
storm
450***[email protected]
xiaomeme
xia***[email protected]
参考方法:
#! /usr/bin/python
# -*- coding:UTF-8 -*-
a = int(raw_input("请输入:"))
b = int(raw_input("请输入:"))
c = int(raw_input("请输入:"))
if a>b and a>c:
x = a
a = c
elif b>a and b>c:
x = b
b = c
else:
x = c
if a>b:
print b,a,x
else:
print a,b,xxiaomeme
xia***[email protected]
晴宇
lia***[email protected]
参考方法:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
x = int(raw_input('x->'))
y = int(raw_input('y->'))
z = int(raw_input('z->'))
arr = [x,y,z]
for i in range (0,3):
for o in range (0,3):
for p in range (0,3):
if arr[i] > arr[o] > arr[p]:
print arr[i],arr[o],arr[p]晴宇
lia***[email protected]
OMG_d
243***[email protected]
参考方法:
# -*- coding: utf-8 -*-
import numpy as np
x=int(input('请输入一个整数:\n'))
y=int(input('请输入一个整数:\n'))
z=int(input('请输入一个整数:\n'))
l = [x,y,z]
for i in range(len(l)):
a = np.argmin(l)
print(l[a])
del l[a]
OMG_d
243***[email protected]
墨雨
lzn***@163.com
下例更简单,Python2.x 与 Python3.x 均可使用:
# -*- coding: utf-8 -*-
x=int(input('请输入一个整数:\n'))
y=int(input('请输入一个整数:\n'))
z=int(input('请输入一个整数:\n'))
if(x > y):
x,y = y,x;
if(y > z):
y,z = z,y
print((x,y,z));
下例更通用:
# -*- coding: utf-8 -*-
elements=(4,3,5,2,6,1,7);
print("正序排列:{}".format(sorted(elements)));
print("倒序排列:{}".format(list(reversed(sorted(elements)))));墨雨
lzn***@163.com
Sa
hap***[email protected]
python3
print(sorted([int(input("enter a integer: ")) for x in range(3)]))Sa
hap***[email protected]
yosning520
135***[email protected]
冒泡算法:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#冒泡排序
a = [int(i) for i in raw_input("请输入:").split()]
m = len(a)
while m!=1:
for i in range(m-1):
if a[i]>a[i+1]:
x = a[i]
a[i] = a[i+1]
a[i+1] = x
m -= 1
print a
列表sort:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
l = [int(i) for i in raw_input("请输入: ").split()]
l.sort()
print lyosning520
135***[email protected]