菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例37

Python 100例 Python 100例

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

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

程序源代码:

实例

#!/usr/bin/python# -*- coding: UTF-8 -*-if__name__ == "__main__": N = 10# input dataprint('请输入10个数字:\n')l = []foriinrange(N): l.append(int(input('输入一个数字:\n')))printforiinrange(N): print(l[i])print# 排列10个数字foriinrange(N - 1): min = iforjinrange(i + 1,N): ifl[min] > l[j]:min = jl[i],l[min] = l[min],l[i]print('排列之后:')foriinrange(N): print(l[i])

以上实例输出结果为:

请输入10个数字:
输入一个数字:
5
输入一个数字:
3
输入一个数字:
23
输入一个数字:
67
输入一个数字:
2
输入一个数字:
56
输入一个数字:
45
输入一个数字:
98
输入一个数字:
239
输入一个数字:
9
5
3
23
67
2
56
45
98
239
9
排列之后:
2
3
5
9
23
45
56
67
98
239

Python 100例 Python 100例

AI 思考中...

11 篇笔记 写笔记

  1. #0

    叮咚

    a12***[email protected]

    38

    参考解析方案:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    print '请输入10个数字:\n'
    a=[]
    for n in range(10):
     a.append(int(raw_input('输入一个数字:\n')))
    a.sort()
    print a
    

    叮咚

    a12***[email protected]

    9年前 (2017年04月09日)
  2. #0

    叮咚

    a12***[email protected]

    3

    参考解析方案:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    print '请输入10个数字:\n'
    a=[]
    for n in range(10):
     a.append(int(raw_input('输入一个数字:\n')))
    for i in range(0,9):
     min=i
     for j in range(i+1,10):
     if a[min]>a[j]:
     min=j
     if min!=i:
     t=a[min]
     a[min]=a[i]
     a[i]=t
    print a
    

    叮咚

    a12***[email protected]

    9年前 (2017年04月09日)
  3. #0

    dayu

    923***[email protected]

    4

    参考方法:

    #!/usr/bin/python
    # coding:utf-8
    a = []
    for i in range(10):
     a.append(input("entert the num:"))
    print a
    for i in range(9):
     for j in range(i+1,10):
     if a[i] > a[j]:
     a[i],a[j] = a[j],a[i]
    print a
    

    dayu

    923***[email protected]

    9年前 (2017年05月18日)
  4. #0

    莫哉啼

    695***[email protected]

    4

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    L=[]
    while len(L)<10:
     n=len(L)+1
     L.append(int(input('输入第%d个数字'%(n))))
    print(L)
    L2=sorted(L)
    print(L2)
    for a in L2:
     print(a)
    

    莫哉啼

    695***[email protected]

    9年前 (2017年05月28日)
  5. #0

    Dr.D

    hah***[email protected]

    6

    参考方法,使用 min+remove 挑选最小数以及弹出最小数方法排序

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    n = 0
    S = []
    T = []
    for num in range(1,11):
     a = int(input("输入: "))
     S.append(a)
    for n in range(1,11):
     b = min(S)
     T.append(b)
     S.remove(b)
    print T
    

    Dr.D

    hah***[email protected]

    9年前 (2017年06月06日)
  6. #0

    红萝卜

    101***[email protected]

    1

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import numpy as np
    a=np.random.randint(1,100,10)
    print '原始随机数据:',
    print a
    def sortq(left,right,a):
     if left>right:
     return
     i=left
     j=right
     key=a[left]
     while(i<j):
     while(i<j and key<=a[j]):
     j-=1
     a[i]=a[j]
     while(i<j and key>=a[i]):
     i+=1
     a[j]=a[i]
     a[j]=key
     sortq(left,i-1,a)
     sortq(i+1,right,a)
     return a
    b=sortq(0,len(a)-1,a)
    print '排序后:',
    print b

    红萝卜

    101***[email protected]

    9年前 (2017年07月26日)
  7. #0

    newbie

    309***[email protected]

    0

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

    #!/usr/bin/python3
    # -*- coding: UTF-8 -*-
    import random
    A = []
    for i in range(10):
     A.append(random.randint(0,100))
    print(A)
    x = A
    for i, j in enumerate(x):
     T = x[i+1:]
     if T == []:
     break
     if x[i] > min(T):
     t = x.index(min(T))
     x[i], x[t] = x[t], x[i]
    print(A)

    newbie

    309***[email protected]

    9年前 (2017年07月28日)
  8. #0

    chengxuyuan

    hdw***[email protected]

    1

    输入任意个数字,并进行升序降序选择。sorted()函数中reverse参数默认为False

    # encoding:utf-8
    '''
    Created on 2017年8月18日
    @author: wangtaoyuan
    '''
    import re
    str = raw_input('请输入若干数字,中间用逗号分割')#可以用任何非数字符号分割
    num = []
    x = re.finditer(r'\d+',str)
    for i in x:
     num.append(int(i.group()))
    f = raw_input('升序输入s,降序输入j。请输入:')
    if f == 'j':
     print sorted(num, reverse=True)
    else:
     print sorted(num)

    chengxuyuan

    hdw***[email protected]

    9年前 (2017年08月18日)
  9. #0

    tlf

    694***[email protected]

    1

    Python3.x 下测试:

    l = input('输入多个个数字,以逗号隔开')
    l = l.split(',')
    l = [int(i) for i in l]
    #l.index(1) 查找1在列表l中的位置
    o = []
    for i in range(0,len(l)):
     o.append(min(l))
     del l[l.index(min(l))]
    print(o)

    tlf

    694***[email protected]

    9年前 (2017年09月14日)
  10. #0

    patty

    pat***[email protected]

    1

    Python3实例:

    list = []
    for i in range(5):
     list.append( int(input('enter num{} number:'.format(i))))
    list.sort()
    print(list)
    

    patty

    pat***[email protected]

    9年前 (2017年09月16日)
  11. #0

    阳光不锈

    173***[email protected]

    4

    参考方法,兼容 Python2.x 与 Python3.x:

    #coding=utf-8
    #递归函数实现冒泡排序
    def f(n,l):
     i=n
     if i==1:
     return l
     else:
     
     for k in range(i-1):
     if l[k]>l[k+1]:
     l[k],l[k+1]=l[k+1],l[k]
     return f(i-1,l) 
    l=[9,0,4,8,5,2,7,6,3,1]
    print(l) 
    print(f(len(l),l))

    阳光不锈

    173***[email protected]

    8年前 (2018年03月04日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /