1.一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
n=0
while True:
sum=(8e-5)*2**n
if sum<8848.13:
n+=1
else:
print(n)
break
- 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
n=int(input('输入月份'))
pre_2=0
pre_1=1
current=1
for x in range(n-2):
pre_2=pre_1
pre_1=current
current=pre_1+pre_2
print('兔子总数为:',current)
- 将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。
while 1:
n = int(input('请输入一个整数:'))
print('%d='%n,end='')
while n>1:
for i in range(2,n+1):
if n%i==0:
n=int(n/i)
if n==1:
print('%d'%i,end='')
else:
print('%d*'%i,end='')
break
print()
- 输入两个正整数m和n,求其最大公约数和最小公倍数。 程序分析:利用辗除法。
m = int(input('输入第一个正整数:'))
n = int(input('输入第二个正整数:'))
x = m
y = n
if m < n:
temp = m
m = n
n = temp
c = m % n
while c != 0:
m = n
n = c
c = m % n
- 一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3. 编程 找出1000以内的所有完数
for i in range(2,1001):
s=i
for j in range(1,i):
if i%j==0:
s-=j
if s==0:
print(i)
6.输入某年某月某日,判断这一天是这一年的第几天? 程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
year=int(input('输入年份:'))
month=int(input('输入月份:'))
day=int(input('输入日:'))
count=0
if month==1:
count=day
elif month==2:
count=31+day
elif month==3 and (year%4==0 and year%100!=0) or (year%400==0):
count=31+29+day
elif month==4:
count=31+29+31+day
elif month==5:
count=31+29+31+30+day
elif month==6:
count=31+29+31+30+31+day
elif month==7:
count=31+29+31+30+31+30+day
elif month==8:
count=31+29+31+30+31+30+31+day
elif month==9:
count=31+29+31+30+31+30+31+31+day
elif month==10:
count=31+29+31+30+31+30+31+31+30+day
elif month==11:
count=31+29+31+30+31+30+31+31+30+31+day
elif month==12:
count=31+29+31+30+31+30+31+31+30+31+30+day
else:
if month==3:
count=31+28+day
if month==4:
count=31+28+31+day
if month==5:
count=31+28+31+30+day
if month==6:
count=31+28+31+30+31+day
if month==7:
count=31+28+31+30+31+30+day
if month==8:
count=31+28+31+30+31+30+31+day
if month==9:
count=31+28+31+30+31+30+31+31+day
if month==10:
count=31+28+31+30+31+30+31+31+30+day
if month==11:
count=31+28+31+30+31+30+31+31+30+31+day
if month==12:
count=31+28+31+30+31+30+31+31+30+31+30+day
print('第',count,'天')
- 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。求输入的四位整数加密后的值
n=int(input('输入一个四位数:'))
ge=n%10
shi=n//10%10
bai=n//100%10
qian=n//1000
print(ge,shi,bai,qian)
ge=(ge+5)%10
shi=(shi+5)%10
bai=(bai+5)%10
qian=(qian+5)%10
ge,shi,bai,qian=qian,bai,shi,ge
print(qian,bai,shi,ge)
- 获取第n个丑数。 什么是丑数: 因子只包含2,3,5的数
n = int (input('想要获取第几个丑数:'))
n1 = n
if n == 1:
print('第1个丑数是1')
for i in range(2,1000):
x = i
while True:
if i % 2 == 0:
i = i // 2
elif i % 3 == 0:
i = i // 3
elif i % 5 == 0:
i = i // 5
else:
break
if i == 1: # 是丑数
n -=1
break
if n == 1:
print('第%d个丑数是:%d' %(n1,x))
break