本文主要是介绍UVa 10056 What is the Probability ? (概率有一个陷阱),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
10056 - What is the Probability ?
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=997
Probability has always been an integrated part of computer algorithms. Where the deterministic algorithms have failed to solve a problem in short time, probabilistic algorithms have come to the rescue. In this problem we are not dealing with any probabilistic algorithm. We will just try to determine the winning probability of a certain player.
A game is played by throwing a dice like thing (it should not be assumed that it has six sides like an ordinary dice). If a certain event occurs when a player throws the dice (such as getting a 3, getting green side on top or whatever) he is declared the winner. There can be N such player. So the first player will throw the dice, then the second and at last the N th player and again the first player and so on. When a player gets the desired event he or she is declared winner and playing stops. You will have to determine the winning probability of one (The I th) of these players.
Input
Input will contain an integer S (S<=1000) at first, which indicates how many sets of inputs are there. The next S lines will contain S sets of inputs. Each line contain an integer N (N<=1000) which denotes the number players, a floating point number p which indicates the probability of the happening of a successful event in a single throw (If success means getting 3 then p is the probability of getting 3 in a single throw. For a normal dice the probability of getting 3 is 1/6), and I (I<=N) the serial of the player whose winning probability is to be determined (Serial no varies from 1 to N). You can assume that no invalid probability (p) value will be given as input.
Output
For each set of input, output in a single line the probability of the I th player to win. The output floating point number will always have four digits after the decimal point as shown in the sample output.
Sample Input:
22 0.166666 1
2 0.166666 2
Sample Output:
0.54550.4545
注意p=0时分母会为0,要特判。
完整代码:
/*0.016s*/#include<cstdio>
#include<cmath>int main()
{int t, n, i;double p;scanf("%d", &t);while (t--){scanf("%d%lf%d", &n, &p, &i);printf("%.4f\n", p == 0 ? p : p * pow(1 - p, i - 1) / (1 - pow(1 - p, n)));}return 0;
}
这篇关于UVa 10056 What is the Probability ? (概率有一个陷阱)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!