题目链接:https://vjudge.net/contest/76505#problem/Q
题目大意:有1个球,进球的概率为p,连续不进k1个球或者连续进k2个球游戏结束。问投球的期望次数。
ps:上面写的应该是ans=p * f[1]+q * g[1]+1
#include<bits/stdc++.h> #define LL long long #define DB double using namespace std; int main(){ int T, cas=1; scanf("%d", &T); while(T--){ DB p, q; int k1, k2; scanf("%lf%d%d", &p, &k1, &k2); if(p<1e-8){ printf("Case %d: %d\n", cas++, k1); continue; } if(1-p<1e-8){ printf("Case %d: %d\n", cas++, k2); continue; } q=1-p; DB c=(1.0-pow(p, k2-1)); DB d=(1.0-pow(p, k2-1))/q; DB e=(1.0-pow(q, k1-1)); DB f=(1.0-pow(q, k1-1))/p; DB x=(c*f+d)*1.0/(1-c*e); DB y=e*x+f; DB ans=p*x+q*y+1; printf("Case %d: %.8f\n", cas++, ans); } return 0; }