dp(i,j)表示扔i次j个硬币朝上的概率,然后就很好想了。。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 500;
int n, k;
double dp[maxn][maxn];
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i <= k; i++) {
for (int j = 0; j <= n; j++) {
dp[i][j] = 0.0;
}
}
dp[0][0] = 1.0;
for (int i = 0; i < k; i++) {
for (int j = 0; j <= n; j++) {
if (j != n) {
dp[i+1][j] += dp[i][j] / 2;
dp[i+1][j+1] += dp[i][j] / 2;
}
else {
dp[i+1][j-1] += dp[i][j] / 2;
dp[i+1][j] += dp[i][j] / 2;
}
}
}
double ans = 0;
for (int i = 0; i <= n; i++) {
ans += dp[k][i]*i;
}
printf("%.8lf\n", ans);
return 0;
}