zoukankan      html  css  js  c++  java
  • 1-22 练习题1-2

     

    (三) 输入三个整数x,y,z,请把这三个数由小到大输出

    In [7]:
    my_list=[]
    for i in range(3):
            x=int(input('input: '))
            my_list.append(x)
    my_list.sort(reverse=False)#reverse是反转
    my_list
    
     
    input: 2342
    input: 1324
    input: 1
    
    Out[7]:
    [1, 1324, 2342]
     

    (四)将一个列表中的数据复制到另外一个列表中

    In [8]:
    a=[1,2,3]
    b=a[:]
    b
    
    Out[8]:
    [1, 2, 3]
     

    (五)暂停一秒输出,并格式化当前时间。使用time模块的sleeo()函数

    In [11]:
    import time
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
    time.sleep(1)
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
    time.sleep(2)
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
    
     
    2019-08-18 20:55:28
    2019-08-18 20:55:29
    2019-08-18 20:55:31
    
     

    (六)打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身,例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

    In [12]:
    for x in range(1,10):
        for y in range(0,10):
            for z in range(0,10):
                s1=x*100+y*10+z
                s2=pow(x,3)+pow(y,3)+pow(z,3)#pow是次方
                if s1==s2:
                    print(s1)
            
    
     
    153
    370
    371
    407
    
     

    (七)输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。

    In [13]:
    s=input('input:')
    letter=0
    space=0
    digit=0
    others=0
    for c in s:
        if c.isalpha():#判断是不是字符
            letter+=1
        elif c.isspace():
            space+=1
        elif c.isdigit():
            digit+=1
        else:
            others+=1
    print('char=%d,space=%d,digit=%d,others=%d,'%(letter,space,digit,others))
    
     
    input:1233sdd131 看看
    char=5,space=1,digit=7,others=0,
    
     

    (八)一球从100米高度落下,每次落地后反跳回原高度的一半:再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

    In [16]:
    h=100
    time=10
    height=[]#反弹高度
    for i in range(2,time+1):
        h/=2
        height.append(h)
    print(sum(height)*2+100)
    print(min(height)/2)
    
     
    299.609375
    0.09765625
    
  • 相关阅读:
    Leetcode 50.Pow(x,n) By Python
    Leetcode 347.前K个高频元素 By Python
    Leetcode 414.Fizz Buzz By Python
    Leetcode 237.删除链表中的节点 By Python
    Leetcode 20.有效的括号 By Python
    Leetcode 70.爬楼梯 By Python
    Leetcode 190.颠倒二进制位 By Python
    团体程序设计天梯赛 L1-034. 点赞
    Wannafly挑战赛9 C-列一列
    TZOJ Start
  • 原文地址:https://www.cnblogs.com/AI-robort/p/11636504.html
Copyright © 2011-2022 走看看