2014北京大学数字媒体技术优秀大学生夏令营上机考试-Do Not Gamble

描述

Have you ever gambled? I wish you not. Being a professional gambler for three years, I learned every trick they play. Gambling is has nothing to do with luck. It is all about cheating.

One of the most elementary tricks is "mercury dice". Instead of being solid, there is a hollow chamber, filled with mercury, inside of the dice. This little modification makes the dice asymmetric, so that, the probability of each outcome is not equal. Imprecisely, which face is up, depends on the previous state.

To simplify the model, we use "mercury coin" here. As we mentioned, the state (head or tail) after tossing depends on the state before. The probability of that the two states are same, is p. For example, if the head side is up now, the probability that, the coin is still head up after tossing is p. And the probability that the tail side come up is 1-p. If p=0.5, it is a common coin. When p≠ 0.5, this coin is mercury coin. Assume the coin is head up now. After we toss Ntimes, what is the probability that, the number of heads is less than a certain number K?

输入
The test file contains multiple lines, and each line is a test case.
Each test cases consists of three numbers
N K p
N
and K are integers, and p is a float number.

N ≤ 20000
K ≤ N
0 ≤ p ≤ 1
输出
For each test case, output the probability that, after tossing N times, the number of heads is less than K. Rounded to the three digit after the decimal point.
样例输入
3 2 0.6
样例输出
0.448
#include<stdio.h>
#include<string.h>

const int MAXN=20010;
int arr[MAXN];

int k=0;
int n=0;
float p=0;

float sum=0;
void zuhe(int num,int start,int len,int *arr)
{
     if(num==0)
     {
         int pre=1;
         float psum=1;
         int i=0;
         for(;i<n;++i)
         {
             if(arr[i]==pre)
             {
                psum*=p;
             }
             else{
                psum*=(1-p);
             }
             pre=arr[i];
             //printf("%d ",arr[i]);
         }
         sum+=psum;
         //printf("\n");
     }
     else if(num>len)
     {
         return;
     }
     else
     {
         arr[start]=1;
         zuhe(num-1,start+1,len-1,arr);
         arr[start]=0;
         zuhe(num,start+1,len-1,arr);
     }
}
int main()
{

    scanf("%d%d%f",&n,&k,&p);
    memset(arr,0,sizeof(arr));
    int j=0;
    for(;j<k;++j)
    {
        zuhe(j,0,n,arr);
    }
    printf("%.3f\n",sum);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值