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

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

Python 练习实例5

Python 100例 Python 100例

题目:输入三个整数x,y,z,请把这三个数由小到大输出。

程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。

程序源代码:

实例(Python 2.0+)

#!/usr/bin/python# -*- coding: UTF-8 -*-l = []foriinrange(3): x = int(raw_input('integer:\n'))l.append(x)l.sort()printl

实例(Python 3.0+)

#!/usr/bin/python3l = []foriinrange(3): x = int(input('integer:\n'))l.append(x)l.sort()print(l)

以上实例输出结果为:

integer:
8
integer:
5
integer:
6
[5, 6, 8]

Python 100例 Python 100例

AI 思考中...

13 篇笔记 写笔记

  1. #0

    somejune

    502***[email protected]

    23

    其他参考解法:

    #!/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]

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

    lv二呆

    may***[email protected]

    17

    参考解法:

    #!/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]

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

    流年细雨

    758***[email protected]

    39

    参考解法:

    #!/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]

    9年前 (2017年04月13日)
  4. #0

    Alen

    554***[email protected]

    12

    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]

    9年前 (2017年04月15日)
  5. #0

    Atom

    tum***@126.com

    24

    使用 列表 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

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

    初学者

    646***[email protected]

    2

    参考方法:

    #!/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]

    9年前 (2017年05月11日)
  7. #0

    storm

    450***[email protected]

    3

    对输入类型进行了控制,如果输入错误,就提示用户,让用户再次输入,直到正确输入整数为止。

    #!/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]

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

    xiaomeme

    xia***[email protected]

    6

    参考方法:

    #! /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,x

    xiaomeme

    xia***[email protected]

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

    晴宇

    lia***[email protected]

    2

    参考方法:

    #!/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]

    9年前 (2017年07月21日)
  10. #0

    OMG_d

    243***[email protected]

    8

    参考方法:

    # -*- 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]

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

    墨雨

    lzn***@163.com

    9

    下例更简单,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

    9年前 (2017年09月06日)
  12. #0
    7

    python3

    print(sorted([int(input("enter a integer: ")) for x in range(3)]))
    9年前 (2017年10月24日)
  13. #0

    yosning520

    135***[email protected]

    6

    冒泡算法:

    #!/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 l

    yosning520

    135***[email protected]

    9年前 (2017年11月05日)

点我分享笔记

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

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