UVA10056 - What is the Probability ?(概率)

2015-01-27 06:24:37 · 作者: · 浏览: 7

UVA10056 - What is the Probability ?(概率)

题目链接

题目大意:有n个人玩游戏,一直到一个人胜出之后游戏就可以结束,要不然就一直从第1个到第n个循环进行,没人一轮,给出每个人胜出的概率为p,问第i个人胜利的概率。

解题思路:第i个人要胜利,那么就可能在第一轮胜利,也可能在第i轮胜利,那么胜利的概率就是q = 1 - p;概率 = q^(i - 1)?p ? (q^n)^0 + q^(i - 1) ? p ? (q^n)^1 + ...+q^(i - 1) ? p ? (q^n)^k (趋进无穷) 把p? q^(i - 1)提出来,中间的式子可以用幂函数的求和函数来求,那么最后推出的公式就是p? q^(i - 1)/(1 - q^n),但是p等于0的时候要特判。

代码:

#include 
   
     #include 
    
      #include 
     
       const double esp = 1e-9; int main () { int T; scanf ("%d", &T); int n, I; double p; while (T--) { scanf ("%d%lf%d", &n, &p, &I); if (p < esp) { printf ("0.0000\n"); continue; } double q = 1.0 - p; double ans = p * pow(q, I - 1)/(1.0 - pow(q, n)); printf ("%.4lf\n", ans); } return 0; }