1.一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
mountain_h = 8848.13 * 1000
paper_h = 0.08
times = mountain_h / paper_h
if times - int(times):
times = int(times) + 1
else:
times = int(times)
print('对折', times, '次之后能达到珠穆朗玛峰的高度') # 110601625
2. 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
# 方法1, 用for循环
pre_1 = 1
pre_2 = 1
current = 0
for i in range(1, 13):
if i < 3:
current = 1
else:
current = pre_1 + pre_2
pre_1, pre_2 = current, pre_1
print(i, '月的兔子总数为', current)
# 方法2: 递归算法
def fibonacci(number):
if number == 1 or number == 2:
return 1
else:
return fibonacci(number - 1) + fibonacci(number - 2)
for month in range(1, 13):
rabbits = fibonacci(month)
print(month, '月的兔子总数为', rabbits)
# 方法3,用列表
rabbits = [1, 1]
for i in range(12):
if i > 1:
current = rabbits[i - 1] + rabbits[i - 2]
rabbits.append(current)
print(i, '月的兔子总数为', rabbits[i])
"""
1 月的兔子总数为 1
2 月的兔子总数为 1
3 月的兔子总数为 2
4 月的兔子总数为 3
5 月的兔子总数为 5
6 月的兔子总数为 8
7 月的兔子总数为 13
8 月的兔子总数为 21
9 月的兔子总数为 34
10 月的兔子总数为 55
11 月的兔子总数为 89
12 月的兔子总数为 144
"""
3. 将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。
# 方法1 循环
in_number = int(input('求Number的质因数,Number = ')) # 输入90
# 判断是不是质数
def is_prime(number_1):
r = int(number_1 ** 0.5) + 1
for val in range(2, r):
if not number_1 % val:
return False
else:
return True
list1 = [] # 保存分解质因数
number_0 = in_number
save_number0_prime = is_prime(number_0) # 判断是不是质数
if save_number0_prime:
print(number_0, '没有质因子')
else:
while not save_number0_prime: # 获取分解质因数的过程
for i in range(2, number_0):
if (not number_0 % i) and is_prime(i):
list1.append(i)
number_0 = int(number_0 / i)
save_number0_prime = is_prime(number_0)
break
else:
list1.append(number_0)
print(in_number, '=', '*'.join([str(ls) for ls in list1])) # 拼接输出
# 90 = 2*3*3*5
# 方法2 递归
# 判断是不是素数
def is_prime(n):
r = int(n**0.5) + 1
for i in range(2, r):
if not n % i:
return False
else:
return True
# 递归函数
def func_re_num(num, ls2):
for i in range(2, num):
if not num % i and is_prime(i):
ls2.append(i)
return func_re_num(int(num / i), ls2) # 进行递归
else:
ls2.append(num)
return
in_num = int(input('求N的分解质因数,N = '))
ls1 = []
if is_prime(in_num):
print('无法分解因数')
else:
func_re_num(in_num, ls1)
print(in_num, '=', '*'.join([str(ls) for ls in ls1]))
4. 输入两个正整数m和n,求其最大公约数和最小公倍数。 程序分析:利用辗除法。
m0 = int(input('m = '))
n0 = int(input('n = '))
m, n = m0, n0
convention = 1
common_multiple = 1
# 判断最大公约数(辗除法)
# 方法1,循环求最大公约数
if m > n:
while True:
if not m % n:
convention = n
break
else:
m, n = n, m % n
else:
while True:
if not m % n:
convention = n
break
else:
m, n = n, m % n
# 方法2,函数递归
def fun_convention(a, b):
if a % b:
return fun_convention(b, a % b)
else:
return b
if m > n:
convention = fun_convention(m, n)
else:
convention = fun_convention(n, m)
# 判断最大公倍数
common_multiple = int(m0 * n0 / convention)
print(convention, common_multiple)
5. 一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3. 编程 找出1000以内的所有完数
sum1 = 1
for i in range(2, 1001):
sum1 = 1
for j in range(2, i):
if not i % j:
sum1 += j
if not i - sum1:
print(i, end=',')
print('\b')
# 6,28,496
6.输入某年某月某日,判断这一天是这一年的第几天? 程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
while True:
try:
now_time = input('请输入当前时间(格式如 1600年02月21号):')
now_time = now_time.replace(' ', '') # 去掉输入法里面的空格
# 字符串变int型
the_year = int(now_time[:4])
the_month = int(now_time[5:7])
the_day = int(now_time[8:10])
# 判断月份是否超出
if not 0 < the_month < 13:
raise Exception('the_month is out of the range', the_month)
# 一个月里31天的月份
months_31 = [1, 3, 5, 7, 8, 10, 12]
months_30 = [4, 6, 9, 11]
months_two = [28, 29]
all_days = 0
# 判断是闰年的条件
year_condition1 = not the_year % 4 and the_day % 100
year_condition2 = not the_year % 400
# 闰年时
if year_condition1 or year_condition2:
if 0 < the_month < 13:
for i_month in range(1, the_month):
if i_month in months_30:
all_days += 30
elif i_month in months_31:
all_days += 31
elif i_month == 2:
all_days += months_two[0]
else:
if the_month in months_30:
if not 0 < the_day < 31:
raise Exception('the_day out of range', the_day)
elif the_month in months_31:
if not 0 < the_day < 32:
raise Exception('the_day out of range', the_day)
elif the_month == 2:
if not 0 < the_day < 29:
raise Exception('the_day out of range', the_day)
all_days += the_day
# 不是闰年
else:
if 0 < the_month < 13:
for i_month in range(1, the_month):
if i_month in months_30:
all_days += 30
elif i_month in months_31:
all_days += 31
elif i_month == 2:
all_days += months_two[1]
else:
if the_month in months_30:
if not 0 < the_day < 31:
raise Exception('the_day out of range', the_day)
elif the_month in months_31:
if not 0 < the_day < 32:
raise Exception('the_day out of range', the_day)
elif i_month == 2:
if not 0 < the_day < 90:
raise Exception('the_day out of range', the_day)
all_days += the_day
print('今天是这一年的第', all_days, '天')
break
except ValueError as e:
print('输入格式不正确')
except Exception as e:
print(e)
# 》》》1600年03月21号
# 今天是这一年的第 80 天
7. 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。求输入的四位整数加密后的值
in_data = input('请输入四位数字:')
str_list = list(in_data)
int_list = list(map(int, str_list))
index = 0
for i in int_list:
int_list[index] = (i + 5) % 10
index += 1
index -= 1
int_list[0], int_list[index] = int_list[index], int_list[0]
int_list[1], int_list[index-1] = int_list[index-1], int_list[1]
md_str_list = list(map(str, int_list))
md_str = ''.join(md_str_list)
md_int = int(md_str)
print(md_int)
# 》》》8152
# 7063
8. 获取第n个丑数。 什么是丑数: 因子只包含2,3,5的数
6 =1* 2 * 3 -> 丑数
2 = 12 -> 丑数
7 = 17 -> 不是丑数
1, 2, 3, 4, 5, 6, 8,9,10, 12 ….
n = int(input('获取第n位丑叔,n = '))
nums = [2, 3, 5]
ugly_numbers = [1]
loc2 = 0
loc3 = 0
loc5 = 0
min_number = 0
len_ugly = 1
while True:
if n == len_ugly:
break
number2 = ugly_numbers[loc2]*2
number3 = ugly_numbers[loc3]*3
number5 = ugly_numbers[loc5]*5
min_number = min(number2, number3, number5)
if min_number not in ugly_numbers:
ugly_numbers.append(min_number)
len_ugly += 1
if number2 == min_number:
loc2 += 1
elif number3 == min_number:
loc3 += 1
elif number5 == min_number:
loc5 += 1
# print(ugly_numbers) # n >>> 20
# 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36
print('第', n, '位丑数的值是', ugly_numbers[n-1])
# 第20位丑数的值是36