24h購物| | PChome| 登入
2013-08-11 20:51:35| 人氣1,830| 回應0 | 上一篇 | 下一篇

[UVA] 12457 - Tennis contest

推薦 0 收藏 0 轉貼0 訂閱站台

 D: Tennis contest 

Background

Nadal or Djokovic? Who is the best one?

The Problem

The two most famous tennis players, A and B, are facing each other in up to 2n-1 matches. The one who wins n matches will be the best player in the world. We supose the result of each game doesn't depend on the rest, and there is a constant likelihood, p, of A to win a match. Draw is an invalid result. Which is in advance the probability of A to win the title?

The Input

The first line of the input contains an integer, t, indicating the number of test cases. For each test case, two lines appear, the first one contains a number n, 1<=n<=25, representing the number of wins A has to reach. The second line contains a number p, 0<=p<=1, representing the probability of A to win a match.

The Output

For each test case  the output should contain a single line with the number representing the probability in advance of A to win the title of best player in the world.

Sample Input

5
25
0.5
25
0.4
25
0.6
15
0.8
10
0.95

Sample Output

0.50
0.08
0.92
1.00
1.00


OMP'12
Facultad de Informatica
Universidad de Murcia (SPAIN)

題目描述:

給定 A 單場贏 B 的機率為 p, 先贏 n 場的人獲勝,問 A 最後贏的機率為何?

題目解法:


設 dp[i][j] 表示 A 贏 i 場, B 贏 j 場,進行轉移。
轉移的時候,如果 i == n or j == n 則忽略不計,由於遊戲已經結束。

最後 result = sigma(dp[n][i])

#include <stdio.h>
#include <math.h>
int main() {
    int testcase;
    int n, i, j, k;
    double p;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %lf", &n, &p);
        double dp[55][55] = {};
        dp[0][0] = 1;
        for(i = 0; i <= n; i++) {
            for(j = 0; j <= n; j++) {
                if(i == n || j == n)    continue;
                dp[i+1][j] += dp[i][j]*p;
                dp[i][j+1] += dp[i][j]*(1-p);
            }
        }
        double ret = 0;
        for(i = 0; i <= n; i++)
            ret += dp[n][i];
        printf("%.2lf\n", ret);
    }
    return 0;
}

台長: Morris
人氣(1,830) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][博弈] 10111 - Find the Winning Move
此分類上一篇:[UVA][積分] 11346 - Probability

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文