Burger - UVa 557 滚动数组 暴力 递推 打表

Burger 

When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was held at the McDonald's restaurant at South Broadway 202, New York. There were 20 kids at the party, including Ben and Bill. Ronald McDonald had made 10 hamburgers and 10 cheeseburgers and when he served the kids he started with the girl directly sitting left of Bill. Ben was sitting to the right of Bill. Ronald flipped a (fair) coin to decide if the girl should have a hamburger or a cheeseburger, head for hamburger, tail for cheeseburger. He repeated this procedure with all the other 17 kids before serving Ben and Bill last. Though, when coming to Ben he didn't have to flip the coin anymore because there were no cheeseburgers left, only 2 hamburgers.


Ronald McDonald was quite surprised this happened, so he would like to know what the probability is of this kind of events. Calculate the probability that Ben and Bill will get the same type of burger using the procedure described above. Ronald McDonald always grills the same number of hamburgers and cheeseburgers.

Input 

The first line of the input-file contains the number of problems  n  , followed by  n  times:


a line with an even number [2,4,6,...,100000], which indicates the number of guests present at the party including Ben and Bill.

Output 

The output consists of  n  lines with on each line the probability (4 decimals precise) that Ben and Bill get the same type of burger.


Note: a variance of $\pm 0.0001$ is allowed in the output due to rounding differences.

Sample Input 

3
6
10
256

Sample Output 

0.6250
0.7266
0.9500

题意:找到掷n-2次硬币中,同一面至少掷n/2次的概率。

思路:递推,转移方程很好理解,另外用到了滚动数组,这样就可以递推到100000,然后打表解决10000之后的。我打表的时候答案精度是0.00003,所以没用的数可能比较多,正负误差在0.0001即可。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
double dp[2][100010],ans[100010],p=0,f2[92]={0.9920,0.9921,0.9921,0.9922,0.9923,0.9923,0.9924,0.9924,0.9925,0.9926,0.9926,0.9927,0.9927,0.9928,0.9929,0.9929,0.9930,0.9930,0.9931,0.9932,0.9932,0.9933,0.9933,0.9934,0.9935,0.9935,0.9936,0.9936,0.9937,0.9938,0.9938,0.9939,0.9940,0.9940,0.9941,0.9941,0.9942,0.9943,0.9943,0.9944,0.9944,0.9945,0.9946,0.9946,0.9947,0.9947,0.9948,0.9949,0.9949,0.9950,0.9950,0.9951,0.9952,0.9952,0.9953,0.9953,0.9954,0.9955,0.9955,0.9956,0.9956,0.9957,0.9958,0.9958,0.9959,0.9959,0.9960,0.9961,0.9961,0.9962,0.9962,0.9963,0.9964,0.9964,0.9965,0.9965,0.9966,0.9967,0.9967,0.9968,0.9968,0.9969,0.9970,0.9970,0.9971,0.9971,0.9972,0.9973,0.9973,0.9974,0.9974,0.9975};
int f[92]={10000,10154,10310,10470,10634,10802,10974,11150,11330,11514,11704,11898,12096,12300,12508,12722,12942,13168,13400,13638,13882,14132,14390,14654,14926,15206,15492,15788,16092,16404,16726,17058,17400,17752,18114,18488,18872,19268,19678,20100,20536,20986,21452,21934,22432,22946,23478,24030,24602,25194,25808,26444,27104,27788,28498,29236,30004,30802,31632,32496,33396,34334,35312,36332,37398,38510,39674,40890,42164,43498,44896,46362,47902,49520,51220,53010,54896,56884,58982,61198,63540,66020,68648,71436,74398,77548,80902,84478,88296,92380,96754,100000};

int main()
{ int t,n,i,j,k,a,b,num=0;
  dp[1][0]=0.25;
  dp[1][1]=0.5;
  dp[1][2]=0.25;
  ans[2]=0;
  ans[4]=0.25;
  for(i=4;i<=10000;)
  { if((i/2)%2)
    { a=1;b=0;}
    else
    { a=0;b=1;}
    dp[a][0]=dp[b][0]*0.25;
    dp[a][1]=dp[b][0]*0.5+dp[b][1]*0.25;
    for(j=2;j<=i;j++)
     dp[a][j]=dp[b][j-1]*0.5+(dp[b][j-2]+dp[b][j])*0.25;
    i+=2;
    for(j=i/2;j<=i;j++)
     ans[i]+=dp[a][j];
  }
  scanf("%d",&t);
  while(t--)
  { scanf("%d",&n);
    if(n<=10000)
    printf("%.4f\n",ans[n]*2);
    else
    { for(i=0;i<92;i++)
       if(n<=f[i])
       { printf("%.4f\n",f2[i]);
         break;
       }
    }
  }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值