1)从random库中选取相应的函数,用蒙特卡罗方法(统计实验方法)求解pi。
py
import random
def estimate_pi(num_experiments):
num_points_in_circle = 0
num_total_points = 0
for _ in range(num_experiments):
x = random.uniform(-1, 1)
y = random.uniform(-1, 1)
distance = x**2 + y**2
if distance <= 1:
num_points_in_circle += 1
num_total_points += 1
pi_estimate = 4 * num_points_in_circle / num_total_points
return pi_estimate
# 设置实验次数
num_experiments = 1000000
# 估算圆周率
pi_estimate = estimate_pi(num_experiments)
print("蒙特卡罗估算的圆周率: ", pi_estimate)
2)一个笼中共有鸡和兔15只,它们的脚一共有40只,问有多少只鸡?有多少只兔?
py
def solve_chicken_and_rabbit(total_count, total_legs):
# 循环遍历可能的鸡的数量,从0到总数
for chicken_count in range(total_count + 1):
rabbit_count = total_count - chicken_count
# 判断当前的鸡兔数量是否符合脚的总数
if (2 * chicken_count + 4 * rabbit_count) == total_legs:
return chicken_count, rabbit_count
# 如果没有找到符合条件的结果,则返回 None
return None
# 输入总数和脚的总数
total_count = 15
total_legs = 40
# 解决问题并打印结果
solution = solve_chicken_and_rabbit(total_count, total_legs)
if solution:
chicken_count, rabbit_count = solution
print("鸡的数量:", chicken_count)
print("兔子的数量:", rabbit_count)
else:
print("无解")
3) "猴子吃桃"问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第十天早上想吃时, 见只剩一个桃子了。编写程序求第一天猴子共摘了多少个桃子?(1534)
py
def calculate_total_peaches(days):
peaches = 1
for _ in range(days):
peaches = (peaches + 1) * 2
return peaches
# 输入天数
days = 9
# 求解第一天的桃子数量
total_peaches = calculate_total_peaches(days)
print("第一天猴子共摘了", total_peaches, "个桃子")
4)羊车门问题。有3扇关闭的门,一扇门后面停着汽车,其余门后是山羊,只有主持人知道每扇门后面是什么。参赛者可以选择一扇门,在开启它之前,主持人会开启另外一扇门,露出门后的山羊,然后允许参赛者更换自己的选择。靖问:参赛者更换选择后能否增加猜中汽车的机会?请用random库对这个随机事件进行实验,分别输出参赛者改变选择和坚持选择获胜的机率。(进行1000次随机实验,结果以百分制概率输出。近似33.3%,66.6%)
py
import random
def simulate_car_goat_game():
# 初始化三扇门,其中一扇有汽车,其余有山羊
doors = ['car', 'goat', 'goat']
# 参赛者随机选择一扇门
player_choice = random.choice([0, 1, 2])
# 主持人在未选中的门中选择一扇有山羊的门
host_choice = random.choice([i for i in range(3) if i != player_choice and doors[i] == 'goat'])
# 参赛者更换选择,选择另一扇未被选择过的门
player_choice = random.choice([i for i in range(3) if i != player_choice and i != host_choice])
# 判断最终参赛者的选择结果
if doors[player_choice] == 'car':
return 'switch_win' # 参赛者更换选择获胜
else:
return 'stick_win' # 参赛者坚持选择获胜
def calculate_win_probabilities(num_simulations):
switch_wins = 0
stick_wins = 0
for _ in range(num_simulations):
result = simulate_car_goat_game()
if result == 'switch_win':
switch_wins += 1
elif result == 'stick_win':
stick_wins += 1
switch_win_prob = (switch_wins / num_simulations) * 100
stick_win_prob = (stick_wins / num_simulations) * 100
print("参赛者更换选择获胜的机率:{:.1f}%".format(switch_win_prob))
print("参赛者坚持选择获胜的机率:{:.1f}%".format(stick_win_prob))
# 进行1000次随机实验
num_simulations = 1000
calculate_win_probabilities(num_simulations)
5)只能由1和它本身整除的整数被称为素数;若一个素数从左向右读与从右向左读是是相同的数,则该素数为回文素数。编程求解2~1000内的所有回文素数
py
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5)+1):
if num % i == 0:
return False
return True
def is_palindrome(num):
return str(num) == str(num)[::-1]
palindrome_primes = []
for num in range(2, 1001):
if is_prime(num) and is_palindrome(num):
palindrome_primes.append(num)
print("2~1000内的所有回文素数:")
print(palindrome_primes)