#方法1:if语句,练习逻辑能力
num1 = int(input('请输入第一个数:'))
num2 = int(input('请输入第二个数:'))
num3 = int(input('请输入第三个数:'))
if num1 >= num2:
if num1 >= num3:
if num2 >= num3:
print(num3,num2,num1)
else:
print(num2,num3,num1)
else:
print(num2,num1,num3)
else:
if num1 >= num3:
print(num3,num1,num2)
elif num2 >= num3:
print(num1,num3,num2)
else:
print(num1,num2,num3)
#方法2:max min函数排序
num1 = int(input('请输入第一个数:'))
num2 = int(input('请输入第二个数:'))
num3 = int(input('请输入第三个数:'))
nums = []
nums.append(num1)
nums.append(num2)
nums.append(num3)
while True:
cur =min(nums)
print(cur)
nums.remove(cur)
if len(nums) == 1:
print(nums[0])
break
#方法3:sort函数排序
num1 = int(input('请输入第一个数:'))
num2 = int(input('请输入第二个数:'))
num3 = int(input('请输入第三个数:'))
nums =[num1,num2,num3]
nums.sort()
print(nums)
#方法4冒泡法排序
nums = [4,3,5,1]
for i in range(len(nums)-1):
for j in range(len(nums)-i-1):
if nums[j] > nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
print(nums)
#方法5:冒泡法排序改进
count_swap = 0
count = 0
nums = [2,3,5,6]
for i in range(len(nums)-1):
flag = False
for j in range(len(nums)-i-1):
count +=1
if nums[j] > nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
flag = True
count_swap +=1
if not flag:
break
print(nums,count_swap,count)
标签:num1,num2,num3,python,nums,print,int,升序,排序
来源: http://blog.51cto.com/6300167/2350879