day5作业

1.一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?

thick = 0.08/1000
n=0
while thick<8848.13:
    n += 1
    thick = thick*2

print("对折",n,"次")

2. 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子, 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

a1 = 0
a2 = 1
month = int(input("输入第几个月"))
for i in range(1,month):
    t = a1 + a2
    a1 = a2
    a2 = t
    print("第",i,"个月",a1)

3. 将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。

num = int(input("请输入正整数"))
k = 2
for i in range(2,num):
    while k != num:
        if num % k == 0:
            print(k,end='*')
            num = int(num/k)
            # print(num)
        else :
            k += 1
print(num)

4. 输入两个正整数m和n,求其最大公约数和最小公倍数。 程序分析:利用辗除法。

m = 45
n = 20
m1 = m
n1 = n 

if m%n == 0 :
    print("最大公约数",n)
    print("最小公倍数",m)
else:
    while m % n!=0:
        t = m%n
        m,n= n,t
    print("最大公约数为",t)
    print("最小公倍数",int(m1*n1/t))

5.一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3. 编程 找出1000以内的所有完数

for num in range(5,1000):
    n = 1
    sum = 0
    while n

6.输入某年某月某日,判断这一天是这一年的第几天? 程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

year = int(input('输入年份'))
month = int(input('输入月份'))
day = int(input('输入日期'))
if (year%4==0 and year%100 ==0) or year%400==0:
    if month == 1:
        print('这是今年第:',day,'天')
    elif month == 2:
        print('这是今年第:',day+31,'天')
    elif month == 3:
        print('这是今年第:',day+31+29,'天')
    elif month == 4:
        print('这是今年第:',day+31+29+31,'天')
    elif month == 5:
        print('这是今年第:',day+31+29+31+30,'天')
    elif month == 6:
        print('这是今年第:',day+31+29+31+30+31,'天')
    elif month == 7:
        print('这是今年第:',day+31+29+31+30+31+30,'天')
    elif month == 8:
        print('这是今年第:',day+31+29+31+30+31+30+31,'天')
    elif month == 9:
        print('这是今年第:',day+31+29+31+30+31+30+31+31,'天')
    elif month == 10:
        print('这是今年第:',day+31+29+31+30+31+30+31+31+30,'天')
    elif month == 11:
        print('这是今年第:',day+31+29+31+30+31+30+31+31+30+31,'天')
    elif month == 12:
        print('这是今年第:',day+31+29+31+30+31+30+31+31+30+31+30,'天')
else:
    if month == 1:
        print('这是今年第:',day,'天')
    elif month == 2:
        print('这是今年第:',day+31,'天')
    elif month == 3:
        print('这是今年第:',day+31+29-1,'天')
    elif month == 4:
        print('这是今年第:',day+31+29+31-1,'天')
    elif month == 5:
        print('这是今年第:',day+31+29+31+30-1,'天')
    elif month == 6:
        print('这是今年第:',day+31+29+31+30+31-1,'天')
    elif month == 7:
        print('这是今年第:',day+31+29+31+30+31+30-1,'天')
    elif month == 8:
        print('这是今年第:',day+31+29+31+30+31+30+31-1,'天')
    elif month == 9:
        print('这是今年第:',day+31+29+31+30+31+30+31+31-1,'天')
    elif month == 10:
        print('这是今年第:',day+31+29+31+30+31+30+31+31+30-1,'天')
    elif month == 11:
        print('这是今年第:',day+31+29+31+30+31+30+31+31+30+31-1,'天')
    elif month == 12:
        print('这是今年第:',day+31+29+31+30+31+30+31+31+30+31+30-1,'天')

7.某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: 每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。求输入的四位整数加密后的值

num = 4567
a = num%10
print(a)
b = int(num/10)%10
print(b)
c = int(num/100)%10
print(c)
d = int(num/1000)%10
print(d)
a = (a+5)%10
b = (b+5)%10
c = (c+5)%10
d = (d+5)%10
print(a*1000 +b*100+c *10+d) 

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