题目
Input
Output
Sample Intput
2
50
100
Sample Output
Case 1: 12.9933758002
Case 2: 8.5431270393
思路: 数学期望题,用记忆化搜索可以完成。按照期望公式进行运算。
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
double dp[110][1005];
double f(int p, int q)
{
if (q >= 1000) return 100.0 / p;
if (dp[p][q] != -1) return dp[p][q];
//本场比赛轮数 +1, 本场失败 +(1.0 - p / 100.0) * f(p, q + 15),
//本场成功却未取得 +(p / 100.0) * (1.0 - q / 1000.0) * f(p, q + 20), 本场成功且取得 +0
dp[p][q] = 1.0 + (1.0 - p / 100.0) * f(p, q + 15) + (p / 100.0) * (1.0 - q / 1000.0) * f(p, q + 20) + 0;
return dp[p][q];
}
int main()
{
int t;
//memset(dp, -1, sizeof(dp));
for (int i = 1; i <= 100; ++i)
for (int j = 1; j <= 1000; ++j)
dp[i][j] = -1;
scanf("%d", &t);
for (int i = 1; i <= t; ++i)
{
int p;
scanf("%d", &p);
printf("Case %d: %.10lf\n", i, f(p, 20));
}
return 0;
}