zoukankan      html  css  js  c++  java
  • python3练习100题——037

    原题链接:http://www.runoob.com/python/python-exercise-example37.html

    题目:对10个数进行排序。

    程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换。

    所以用冒泡法:

    def compare():
        ls=[]
        while True:
            a=input("please enter a number:")
            if not a:
                break                #break设置任意输入,不管输入多少个值都可以比
            ls.append(int(a))
        for i in range(0,len(ls)):
            for j in range(0,len(ls)-i-1):
                if ls[j]<ls[j+1]:
                    ls[j],ls[j+1]=ls[j+1],ls[j]
        print(ls)
    
    if __name__ =='__main__':
        compare()

    之前做过一次排序,当时就学习了下冒泡法,理解了思路之后,再次应用就容易了很多。

  • 相关阅读:
    MongoDB ObjectId
    MongoDB固定集合
    MongoDB 正则表达式
    MongoDB Map Reduce
    MongoDB操作
    VIM跳到指定行
    linux之echo命令
    rpm and yum commands
    CentOS 7 下的软件安装建议
    apt系统中sources.list文件的解析
  • 原文地址:https://www.cnblogs.com/drifter/p/9207287.html
Copyright © 2011-2022 走看看