美文网首页
作业-第一周末

作业-第一周末

作者: RiskHY | 来源:发表于2019-01-04 17:43 被阅读0次

    周末

    1.控制台输入年龄,根据年龄输出不同的提示(例如: 老年人,青壮年,成年人,未成年,儿童)
    age = int(input('输入年龄:'))
    if age < 12:
        print('儿童')
    elif age < 18:
        print('未成年')
    elif 18 <= age < 40:
        print('青壮年')
    elif age < 60:
        print('成年人')
    else:
        print('老年人')
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    输入年龄:20
    青壮年
    
    Process finished with exit code 0
    
    2.计算5的阶乘,5!的结果是
    num = 5
    sum1 = 1
    while num > 0:
        sum1 *= num
        num -= 1
    print(sum1)
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    120
    
    Process finished with exit code 0
    
    3.求1 + 2!+3!+... + 20!的和,1.程序分析:此程序只是把累加变成了累乘。
    num = 20
    sums = 0
    for nu in range(num, 0, -1):
        amms = 1
        while nu > 1:
            amms *= nu
            nu -= 1
        sums += amms
    print('和为:',sums)
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    和为: 2561327494111820313
    
    Process finished with exit code 0
    
    4.计算1 + 1 / 2!+1 / 3!+1 / 4!+...1 / 20 !=?
    num = 20
    sums = 0
    for nu in range(num, 0, -1):
        amms = 1
        while nu > 1:
            amms *= nu
            nu -= 1
        sums += 1/amms
    print('和为:',sums)
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    和为: 1.718281828459045
    
    Process finished with exit code 0
    
    5.循环输入大于0的数字进行累加,直到输入的数字为0,就结束循环,并最后输出累加的结果。
    num = float(input('输入数字:'))
    sums = 0
    while num != 0:
        sums += num
        num = float(input('输入数字:'))
    print(sums)
    

    结果如下:
    `
    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    输入数字:13
    输入数字:17
    输入数字:5
    输入数字:8
    输入数字:0
    43.0

    Process finished with exit code 0
    `

    6.求s = a + aa + aaa + aaaa + aa...a的值,其中a是一个数字。

    例如2 + 22 + 222 + 2222 + 22222(此时共有5个数相加),几个数相加有键盘控制。
    程序分析:关键是计算出每一项的值。

    count = input('输入叠加数:')
    num = int(input('输入叠加次数:'))
    sums = 0
    for index in range(1, num +1):
        amms = int(count * index)
        sums += amms
    print(sums)
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    输入叠加数:5
    输入叠加次数:5
    61725
    
    Process finished with exit code 0
    
    7.输入三个整数x, y, z,请把这三个数由小到大输出。
    x = int(input('第一个数:'))
    y = int(input('第二个数:'))
    z = int(input('第三个数:'))
    list1 = [x, y, z]
    list1.sort()
    for index in range(3):
        if index < 2:
            print(list1[index], end=', ')
        else:
            print(list1[index])
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    第一个数:45
    第二个数:33
    第三个数:92
    33, 45, 92
    
    Process finished with exit code 0
    
    8.控制台输出三角形
    # a.根据n的值的不同,输出相应的形状
    # n = 5时         n = 4
    # *****           ****
    # ****            ***
    # ***             **
    # **              *
    # *
    
    n = int(input('输入值:'))
    for index in range(n):
        print('*' * (n - index))
    
    # b.根据n的值的不同,输出相应的形状(n为奇数)
    # n = 5            n = 7
    #   *                *
    #  ***              ***
    # *****            *****
    #                 *******
    
    n = int(input('输入值:'))
    for index in range(1, n+1, 2):
        print(' ' * ((n - index) // 2), end='')
        print('*' * index)
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    输入值:5
    *****
    ****
    ***
    **
    *
    
    Process finished with exit code 0
    
    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    输入值:7
       *
      ***
     *****
    *******
    
    Process finished with exit code 0
    
    9.输出9 * 9口诀。

    程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

    for index in range(1, 10):
        for num in range(1, index+1):
                print('%d * %d = %d'%(num, index,index*num), end=' ')
        print()
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    1 * 1 = 1 
    1 * 2 = 2 2 * 2 = 4 
    1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 
    1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16 
    1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 
    1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 
    1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 
    1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 
    1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81 
    
    Process finished with exit code 0
    
    10. 这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?
    for index1 in range(0, 101, 1):
        for index2 in range(0, 101, 1):
            for index3 in range(0, 101, 2):
                if index1 + index2 + index3 == 100 and index1 * 3 + index2 * 2 + index3 * 0.5 == 100:
                    print('大马:%d,中马:%d,小马:%d'%(index1, index2, index3))
    

    结果如下:

    E:\LHYPython\day7homework\venv\Scripts\python.exe E:/LHYPython/day7homework/07-job.py
    大马:2,中马:30,小马:68
    大马:5,中马:25,小马:70
    大马:8,中马:20,小马:72
    大马:11,中马:15,小马:74
    大马:14,中马:10,小马:76
    大马:17,中马:5,小马:78
    大马:20,中马:0,小马:80
    
    Process finished with exit code 0
    
    11.我国古代数学家张邱建在《算经》中出了一道“百钱买百鸡”的问题,题意是这样的:5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。
    for index1 in range(0, 101):
        for index2 in range(0, 101):
            for index3 in range(0, 101):
                if index1 + index2 + index3 == 100 and index1 / 5 + index2 / 3 + 3 * index3 == 100:
                    print('公鸡:%d,母鸡:%d,小鸡:%d' % (index1, index2, index3))
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    公鸡:0,母鸡:75,小鸡:25
    公鸡:20,母鸡:54,小鸡:26
    公鸡:40,母鸡:33,小鸡:27
    公鸡:60,母鸡:12,小鸡:28
    
    Process finished with exit code 0
    
    12.小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。要把100元整好花掉,可如有哪些购买结合?
    for index1 in range(0, 101):
        for index2 in range(0, 101):
            for index3 in range(0, 101):
                if index1*15 + index2*2 + index3*5 == 100:
                    print('洗发水:%d,香皂:%d,牙刷:%d' % (index1, index2, index3))
    

    结果如下:

    E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/venv/02-job.py
    洗发水:0,香皂:0,牙刷:20
    洗发水:0,香皂:5,牙刷:18
    洗发水:0,香皂:10,牙刷:16
    洗发水:0,香皂:15,牙刷:14
    洗发水:0,香皂:20,牙刷:12
    洗发水:0,香皂:25,牙刷:10
    洗发水:0,香皂:30,牙刷:8
    洗发水:0,香皂:35,牙刷:6
    洗发水:0,香皂:40,牙刷:4
    洗发水:0,香皂:45,牙刷:2
    洗发水:0,香皂:50,牙刷:0
    洗发水:1,香皂:0,牙刷:17
    洗发水:1,香皂:5,牙刷:15
    洗发水:1,香皂:10,牙刷:13
    洗发水:1,香皂:15,牙刷:11
    洗发水:1,香皂:20,牙刷:9
    洗发水:1,香皂:25,牙刷:7
    洗发水:1,香皂:30,牙刷:5
    洗发水:1,香皂:35,牙刷:3
    洗发水:1,香皂:40,牙刷:1
    洗发水:2,香皂:0,牙刷:14
    洗发水:2,香皂:5,牙刷:12
    洗发水:2,香皂:10,牙刷:10
    洗发水:2,香皂:15,牙刷:8
    洗发水:2,香皂:20,牙刷:6
    洗发水:2,香皂:25,牙刷:4
    洗发水:2,香皂:30,牙刷:2
    洗发水:2,香皂:35,牙刷:0
    洗发水:3,香皂:0,牙刷:11
    洗发水:3,香皂:5,牙刷:9
    洗发水:3,香皂:10,牙刷:7
    洗发水:3,香皂:15,牙刷:5
    洗发水:3,香皂:20,牙刷:3
    洗发水:3,香皂:25,牙刷:1
    洗发水:4,香皂:0,牙刷:8
    洗发水:4,香皂:5,牙刷:6
    洗发水:4,香皂:10,牙刷:4
    洗发水:4,香皂:15,牙刷:2
    洗发水:4,香皂:20,牙刷:0
    洗发水:5,香皂:0,牙刷:5
    洗发水:5,香皂:5,牙刷:3
    洗发水:5,香皂:10,牙刷:1
    洗发水:6,香皂:0,牙刷:2
    洗发水:6,香皂:5,牙刷:0
    
    Process finished with exit code 0
    

    相关文章

      网友评论

          本文标题:作业-第一周末

          本文链接:https://www.haomeiwen.com/subject/ehukrqtx.html