1175: The Binomial Function

动态规划问题 注意输出格式

 

#include<stdio.h>
#include<memory.h>
#define N 31
int used[N][N];
int Binom(int n,int m){
 if(n==0||n==m||m==0)
  return 1;
 else{
  if(used[n][m]){
   return used[n][m];
  }
  else {
   used[n][m]=Binom(n-1,m)+Binom(n-1,m-1);
   return used[n][m];
  }
 }
}
int main(){
 int n,m,v;
 while((scanf("%d%d",&n,&m))!=EOF){
  if(m==0&&n==0)
   return 0;
  else{
   v=Binom(n,m);
   printf("Binom(%d, %d) = %d/n",n,m,v);
  }
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's a Python implementation of the Monty Hall problem: ```python import numpy as np import matplotlib.pyplot as plt def random_door(): # sample 1 door randomly from 3 doors return np.random.choice([1, 2, 3]) def monty_choice(contestant_choice, car_door): # Monty chooses a door to open conditional on the contestant's choice and the car's location if contestant_choice == car_door: # if the contestant has selected the door with the car, flip a fair coin to choose which door Monty opens return np.random.binomial(1, 0.5) + 1 else: # otherwise, open the door that is not selected by the contestant or the one with the car return list(set([1, 2, 3]) - set([contestant_choice, car_door]))[0] def win_car(strategy, contestant_choice, car_door, monty_choice): # determine if the contestant wins the car, conditional on the strategy, the contestant's choice, the car's location, and Monty's choice if strategy == "switch": # if the contestant switches doors, select the other unopened door new_choice = list(set([1, 2, 3]) - set([contestant_choice, monty_choice]))[0] return new_choice == car_door else: # otherwise, stick with the original choice return contestant_choice == car_door def simulation(n, strategy): # run the simulation n times with the given strategy car_door = random_door() contestant_choice = random_door() successes = 0 failures = 0 for i in range(n): monty = monty_choice(contestant_choice, car_door) if win_car(strategy, contestant_choice, car_door, monty): successes += 1 else: failures += 1 return successes, failures # run the simulation for each strategy n = 1000 switch_successes, switch_failures = simulation(n, "switch") no_switch_successes, no_switch_failures = simulation(n, "noswitch") # plot the results fig, axes = plt.subplots(1, 2, figsize=(10, 5)) axes[0].bar(["Success", "Failure"], [switch_successes, switch_failures]) axes[0].set_title("Switch Strategy") axes[1].bar(["Success", "Failure"], [no_switch_successes, no_switch_failures]) axes[1].set_title("No Switch Strategy") plt.show() ``` In this implementation, the `random_door()` function randomly selects a door for the car and the contestant's initial choice. The `monty_choice()` function chooses the door for Monty to open, given the contestant's choice and the car's location. The `win_car()` function determines whether the contestant wins the car, given the strategy, the contestant's choice, the car's location, and Monty's choice. Finally, the `simulation()` function runs the simulation `n` times with a given strategy and returns the number of successes and failures. According to the simulation results, the contestant should switch doors to maximize their chances of winning the car. The switch strategy has a success rate of around 66.5%, while the no switch strategy has a success rate of around 33.5%. This result is consistent with the conditional probability of the problem, which shows that the probability of winning the car increases from 1/3 to 2/3 if the contestant switches doors.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值