3.8作业

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

thick = 0.00008
count = 0
while thick < 8848.13:
    count += 1
    thick = thick * 2 
print('折叠:',count,'次')

print('====================================================')

  1. 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
lapin = 1
lapin2 = 1
lapin3 = 0
month = int(input('请输入第几个月:'))
if month < 3:
    print(lapin)
else:
    for n in range(0,month-2):
        lapin3 = lapin + lapin2
        lapin = lapin2
        lapin2 = lapin3
    print(lapin3)

print('=====================================================')

3.将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。(没有排除输入的数是质数的情况,能力有限)

num = int(input('请输入一个数:'))
num1 = num 
n = 2 
print(num1,'=',end = '')
while  n < num1:
    if num % n == 0:
        print(n,end = ' x ')
        num = num // n
        if n == num:
            print(num)
            break
    elif n < num:
        n += 1
        if n == num:
            print(num)
            break

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

m = int(input('请输入一个正整数:'))
n = int(input('请输入一个正整数:'))
n1 = n 
m1 = m 
k = 0 
if m < n:
    m,n = n,m 
while m > n:
    k = m % n 
    if k == 0:
        print('最大公约数是:',n)
        break
    else:
        m = n
        n = k
print('最大公倍数是:',m1*n1/n)
  1. 一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3. 编程 找出1000以内的所有完数
    print('===========================================')
sum1 = 0
print('完数有:')
for n in range(1,1000):
    for m in range(1,n):
        if n % m == 0:
            sum1 += m
    if sum1 == n:
        print(n)
    sum1 = 0

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

year = int(input('输入年份'))
month = int(input('输入月份'))
day = int(input('输入日期'))

"""year = 1999
month = 7
day = 23
"""
if ((year % 100 == 0) and (year % 400 == 0)) or ((year % 100 != 0) and (year % 4 == 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,'天')
  1. 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。求输入的四位整数加密后的值
num = int(input('请输入一个四位数'))
a = 0
b = 0
c = 0
d = 0
a = (num // 1000 + 5) % 10
b = (num // 100 % 10 + 5) % 10
c = (num % 100 // 10 + 5) % 10
d = (num % 10 + 5) % 10
print(a,b,c,d)
a,b,c,d,= d,c,b,a
print(a,b,c,d)
num =a * 1000 + b * 100 + c * 10 + d 
print('加密后的值是:',num)

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