day5-作业

基础:读程序,总结功能

numbers = 1
for i in range(0,20):
    numbers *= 2
print(numbers)

功能:求2^20

summation = 0
num = 1
while num <= 100:
    if(num % 3 == 0 or num % 7 == 0) and num % 21 != 0:
        summation += 1
    num += 1
print(summation)

功能:求1到100中能被3或7整除但不能被3和7同时整除的数的个数

1.1-100之间所有数的和、平均值

sum1 = 0
for num in range(101):
    sum1 += num
print(sum1)#5050
print(sum1/100)#50.5

num = 1
sum1 = 0
while num <= 100:
    sum1 += num
    num += 1
print(sum1)#5050
print(sum1/100)#50.5

2.1-100之间能被3整除的数的和

sum1 = 0
for num in range(101):
    if num % 3 == 0:
        sum1 += num
print(sum1)
sum2 = 0
num = 0
while num <101:
    if num % 3 == 0:
        sum2 += num
    num += 1
print(sum2)

3.1-100之间不能被7整除的数的和

sum3 = 0
for num1 in range(1,101):
    if num1 % 7 != 0:
        sum3 += num1
print(sum3)
sum1 = 0
num = 0
while num < 101:
    if num % 7 != 0:
        sum1 += num
    num += 1
print(sum1)

稍困难

1.求斐波那契数列中第n个数的值:1,1,2,3,5,8,13,21,34....

n = int(input('请输入:'))
num = 2
sum1 = 0
sum2 = 1
while num <= n:
    sum1 += sum2
    sum2 += sum1
    num += 2
if n%2==0:
    print(sum1)
else:
    print(sum2)

2.判断101-200之间有多少个不是素数

import math
count = 0
for num in range(101,201):
    num1 = 2
    while num1<= num/2:
        if num % num1 == 0:
            count += 1
            break
        num1 += 1
print(count)

3.打印所有水仙花数

for num in range(100,1000):
    num1 = num % 10
    num2 = num // 10 % 10
    num3 = num //100
    if num == num1**3 + num2**3 + num3**3:
        print(num)#153,370,371,407

4.有一分数数列,2/1,3/2,5/3,8/5...求第二十个分数

fz = 2
fm = 1
for _ in range(19):
    fz += fm
    fm = fz - fm
print('第二十个分数是:%d / %d' % (fz,fm))    
fz = [2]
fm = [1]
for index in range(19):
  fz.append(fz[index] + fm[index])
  fm.append(fz[index])
print(fz[19]/fm[19])

5.给一个正整数,求它是几位数,逆序打印各位数字

num = input('请输入一个正整数:')
str1 = str(num)
print('它是一个%d位数' % (len(str1)))
print('它的逆序是:%s' % str1[::-1])

1.控制台输入年龄,根据年龄输出不同的提示(例如:老年人,青壮年,成年人,未成年,儿童)

age = int(input('请输入年龄:'))
if age < 10:
    print("儿童")
elif age < 18:
    print('未成年')
elif age < 22:
    print('成年人')
elif age < 50:
    print('青壮年')
else:
    print('老年人')

2.计算5的阶乘 5!的结果是

sum1 = 1
for num in range(1,6):
    sum1 *= num
print(sum1)

3.求1+2!+3!+...+20!的和 1.程序分析:此程序只是把累加变成了累乘

sum2 = 0
for num1 in range(1,21):
    sum1 = 1
    for num2 in range(1,num1+1):
        sum1 *= num2
    sum2 += sum1
print(sum2)

4.计算 1+1/2!+1/3!+1/4!+...1/20!=?

sum2 = 0
for num1 in range(1,21):
    sum1 = 1
    for num2 in range(1,num1+1):
        sum1 *= num2
    sum2 += 1/sum1
print(sum2)

5.循环输入大于0的数字进行累加,直到输入的数字为0,就结束循环,并最后输出累加的结果。

sum1 = 0
while num != 0:
    num = int(input('请输入:'))
    sum1 += num
print(sum1)
str1 = ''
sum1 = 0
num = int(input('请输入:'))
for num1 in range(1,num+1):
    str1 = '2' + str1
    sum1 += int(str1)
print(sum1)

6.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

1.程序分析:关键是计算出每一项的值。

str1 = ''
sum1 = 0
num = int(input('请输入:'))
for num1 in range(1,num+1):
    str1 = '2' + str1
    sum1 += int(str1)
print(sum1)

7.输入三个整数x,y,z,请把这三个数由小到大输出。

x = int(input('请输入x'))
y = int(input('请输入y'))
z = int(input('请输入z'))
if x < y:
    if y < z:
        max = z
        min = x
        middle = y
    elif x < z:
        max = y
        min = x
        middle = z
    else:
        max = y
        min = z
        middle = x
elif x < z:
    max = z
    min = y
    middle = x
elif y < z:
    max = x
    min = y
    middle = z
else:
    max = x
    min = z
    middle = y
print('从小到大排列为:%d,%d,%d' % (min,middle,max))

8.控制台输出三角形

num = int(input('请输入一个正整数:'))
str1 = '*'
while num >= 1:
    print(str1*num)
    num -= 1
num = int(input('请输入一个奇数:'))
str1 = '*'
num1 = 1
while num1 <= num:
    str2 = str1*num1
    print(str2.center(num))
    num1 += 2

9.输出9*9口诀。 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

for i in range(1,10):
    for j in range(1,i+1):
        print("%s x %s = %d" % (i,j,i*j),end=' ')
    print('')

10.这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?

11.我国古代数学家张邱建在《算经》中出了一道“百钱买百鸡”的问题,题意是这样的: 5文钱可以买一只公鸡,3文钱可以买一只母鸡,

1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。

12.小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。

要把100元整好花掉,可如有哪些购买结合?

for x in range(101):
    for y in range(101-x):
        for z in range(101-x-y):
            if 3*x + 2*y +0.5*z == 100 and x + y + z == 100:
                print(x,y,z)
for x1 in range(101):
    for y1 in range(101-x1):
        for z1 in range(101-x1-y1):
            if 3*y1 + 5*x1 + 1/3*z1 == 100 and x1 +y1 + z1 == 100:
                print(x1,y1,z1)
for x11 in range(101):
    for y11 in range(101-x11):
        for z11 in range(101-x11-y11):
            if 15*x11 +2*y11 + 5*z11 == 100:
                print(x11,y11,z11)

你可能感兴趣的:(day5-作业)