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個月起每個月都生一對兔子渺氧,小兔子長到第三個月后每個月又生一對兔子旨涝,假如兔子都不死,問每個月的兔子總數(shù)為多少?
# 方法1白华, 用for循環(huán)
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, '月的兔子總數(shù)為', 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, '月的兔子總數(shù)為', 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, '月的兔子總數(shù)為', rabbits[i])
"""
1 月的兔子總數(shù)為 1
2 月的兔子總數(shù)為 1
3 月的兔子總數(shù)為 2
4 月的兔子總數(shù)為 3
5 月的兔子總數(shù)為 5
6 月的兔子總數(shù)為 8
7 月的兔子總數(shù)為 13
8 月的兔子總數(shù)為 21
9 月的兔子總數(shù)為 34
10 月的兔子總數(shù)為 55
11 月的兔子總數(shù)為 89
12 月的兔子總數(shù)為 144
"""
3. 將一個正整數(shù)分解質因數(shù)。例如:輸入90,打印出90=2x3x3x5弧腥。
# 方法1 循環(huán)
in_number = int(input('求Number的質因數(shù)厦取,Number = ')) # 輸入90
# 判斷是不是質數(shù)
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 = [] # 保存分解質因數(shù)
number_0 = in_number
save_number0_prime = is_prime(number_0) # 判斷是不是質數(shù)
if save_number0_prime:
print(number_0, '沒有質因子')
else:
while not save_number0_prime: # 獲取分解質因數(shù)的過程
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 遞歸
# 判斷是不是素數(shù)
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
# 遞歸函數(shù)
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的分解質因數(shù),N = '))
ls1 = []
if is_prime(in_num):
print('無法分解因數(shù)')
else:
func_re_num(in_num, ls1)
print(in_num, '=', '*'.join([str(ls) for ls in ls1]))
4. 輸入兩個正整數(shù)m和n管搪,求其最大公約數(shù)和最小公倍數(shù)虾攻。 程序分析:利用輾除法。
m0 = int(input('m = '))
n0 = int(input('n = '))
m, n = m0, n0
convention = 1
common_multiple = 1
# 判斷最大公約數(shù)(輾除法)
# 方法1更鲁,循環(huán)求最大公約數(shù)
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霎箍,函數(shù)遞歸
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)
# 判斷最大公倍數(shù)
common_multiple = int(m0 * n0 / convention)
print(convention, common_multiple)
5. 一個數(shù)如果恰好等于它的因子之和,這個數(shù)就稱為 "完數(shù) "澡为。例如6=1+2+3. 編程 找出1000以內的所有完數(shù)
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. 某個公司采用公用電話傳遞數(shù)據(jù),數(shù)據(jù)是四位的整數(shù)谋旦,在傳遞過程中是加密的剩失,加密規(guī)則如下:每位數(shù)字都加上5,然后用和除以10的余數(shù)代替該數(shù)字,再將第一位和第四位交換册着,第二位和第三位交換赴叹。求輸入的四位整數(shù)加密后的值
in_data = input('請輸入四位數(shù)字:')
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個丑數(shù)。 什么是丑數(shù): 因子只包含2指蚜,3,5的數(shù)
6 =1* 2 * 3 -> 丑數(shù)
2 = 12 -> 丑數(shù)
7 = 17 -> 不是丑數(shù)
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, '位丑數(shù)的值是', ugly_numbers[n-1])
# 第20位丑數(shù)的值是36