內容
有一群人,輪流骰骰子(不一定是六面骰),算出指定的人贏的機率。
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.
第一行S(S ≤ 1000)
接下來S行有3三個數字
N(N ≤ 1000) p I(I ≤ N)
N是總參加人數
p是該骰子贏的機率
I是指定的人
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.
每一行輸出皆為小數點後4位
Sample Input
2
2 0.166666 1
2 0.166666 2
Sample Output
0.5455
0.4545
C++
#include<iostream> #include <cmath> #include <iomanip> using namespace std; int s; int main() { while(cin>>s)//測資數量 { int n,i; long double p; while(s--) { cin>>n>>p>>i;//輸入數字 int round=0; long double nowp=1,ans=0;//nowp是當前回合的贏機率 do { nowp=pow((1-p),round*n)*pow((1-p),i-1)*p;//pow((1-p),round*n) 是玩到第round+1輪的機率 //pow((1-p),i-1)是該輪前面的人輸的機率 //p 是自己勝率的機率 ans+=nowp;//把每回合的贏的機率相加就是答案 round++; }while(nowp>0.00000000000001);//0多打幾個,才算的準 cout<<fixed<<setprecision(4)<<ans<<endl;; } } return 0; }
python
from decimal import getcontext, Decimal import decimal s = int(input()) for i in range(s): n = input() n = n.split() p = float(n[1]) round = 0 nowp = 0.000000 ans = 0.000 nowp = ((1-p)**(int(n[2])-1))*p while nowp > 0.0000000000001: ans += nowp round += 1 nowp = ((1-p)**(round*int(n[0])))*((1-p)**(int(n[2])-1))*p ans = Decimal(ans).quantize(Decimal('0.0000')) print(ans)