AOJ-AHU-OJ-294 NBA Finals

NBA Finals
Time Limit: 1000 ms   Case Time Limit: 1000 ms   Memory Limit: 64 MB
Description
Consider two teams, Lakers and Celtics, playing a series of NBA Finals until one of the teams wins n games. Assume that the probability of Lakers winning a game is the same for each game and equal to p and the probability of Lakers losing a game is q = 1-p. Hence, there are no ties.Please find the probability of Lakers winning the NBA Finals if the probability of it winning a game is p.

Input
1st line: the game number (7<=n<=165) the winner played.
2nd line: the probability of Lakers winning a game p (0<p<1).

Output
1st line: The probability of Lakers winning the NBA Finals.
Round the result to 6 digits after the decimal point and keep trailing zeros.

Sample Input
OriginalTransformed
7
0.4

Sample Output
OriginalTransformed
0.228844

——————————————————刚吃饱了的分割线——————————————————

思路:简单分析之后,知道了此题涉及两个问题:1.N局M胜(M=N/2+1)制 2.组合数

先解决组合数的问题。很明显,涉及阶乘。阶乘弄不好就会超时或者溢出。解决方法:打表。打表避免了递归。组合数的打表模板是这样的:

Com[0][0] = 1;
    for(int i = 1; i < MAXN; i++)
        for(int j = 0; j <= i; j++){
            if(!j || j==i)  Com[i][j] = 1;
            else  Com[i][j] = Com[i-1][j] + Com[i-1][j-1];
        }

其次是排列组合的问题。对于N局M胜的概率问题,当时我推导了一遍公式。经验是:肯定是以胜局结尾。然后按一共打了几场分类讨论。公式如下:

i = 0 ——> i = game-win-1
    ans += Com[game-i-1][win-1] × p^win × (1-p)^(game-win-i);
代码如下:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAXN 330
double Com[MAXN][MAXN];
double power(double a, int n)
{
    double u = 1;
    while(n--)  u *= a;
    return u;
}
int main(){
    int win, game;//game表示是N局,win表示M胜
    double p;
    Com[0][0] = 1;
    for(int i = 1; i < MAXN; i++)
        for(int j = 0; j <= i; j++){
            if(!j || j==i)  Com[i][j] = 1;
            else  Com[i][j] = Com[i-1][j] + Com[i-1][j-1];
        }
    while(scanf("%d%lf", &win, &p)!=EOF){
        double ans = 0;
        game = win*2 - 1;
        ans += power(p, win);//首先算出打了M场的时候,概率的值
        for(int i = 0; i < game-win; i++)
            ans += Com[game-i-1][win-1] * power(p, win) * power(1-p, game-win-i);
        printf("%.6lf\n", ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值