LightOJ - 1408 Batting Practice
Batting Practice
After being all out for 58 and 78 in two matches in the most prestigious tournament in the world, the coach of a certain national cricket team was very upset. He decided to make the batsmen practice a lot. But he was wondering how to make them practice, because the possibility of getting out seems completely random for them. So, he decided to keep them in practice as long as he can and told them to practice in the net until a batsman remains not-out for k1
For a batsman the probability of being out in a ball is independent and is equal to p. What is the expected number of balls he must face to remain not out for k1 consecutive balls or become out in consecutive k2 balls.
Input
Input starts with an integer T (≤ 15000), denoting the number of test cases.
Each case starts with a line containing a real number p (0 ≤ p ≤ 1)
Output
For each case, print the case number and the expected number of balls the batsman will face. Errors less than 10-2 will be ignored.
Sample Input
5
0.5 1 1
0.5 1 2
0.5 2 2
0.19 1 3
0.33 2 1
Sample Output
Case 1: 1
Case 2: 1.5
Case 3: 3
Case 4: 1.2261000000
Case 5: 1.67
閑來無事,做做數學題也是極好的,我只是感覺數學很有有趣罷了
獨立事件的概率是p,如果連續成功的k1次或者連續失敗k2次結束,問需要多少次的期望值
設f(x)為連續成功x次的期望,g(x)為連續失敗x次的期望
f(k1)=g(k2)=0
然後根據期望的公式推啊,我要剛開始成功的概率已經是1了,那麽期望值肯定是k2,因為我不可能失敗,但是我成功的幾率接近於0,那麽我只要成功k1次就好了
#include <bits/stdc++.h> using namespace std; int main(){ int T,ca=1; scanf("%d",&T); while(T--){ double p,q; int k1,k2; scanf("%lf%d%d",&p,&k1,&k2); q=1-p; if(p<1e-6) printf("Case %d: %d\n",ca++,k1); else if(q<1e-6) printf("Case %d: %d\n",ca++,k2); else{ double a=1-pow(q,k1-1),b=1-pow(p,k2-1); double x=(a*b/q+a/p)/(1-a*b),y=b*x+b/q; printf("Case %d: %f\n",ca++,p*y+q*x+1);} } return 0;}
LightOJ - 1408 Batting Practice