uva 11176 - Winning Streak(期望)

题目链接:uva 11176 - Winning Streak


题目大意:给出n和p,有n场比赛,获胜的概率为p,问说连续获胜的期望值是多少。


解题思路:昨天刚做了一道连续获胜的题目uva 10328 - Coin Toss,这不过这道题目始求期望,不过可以通过那道题目的思路求出概率,然后求期望就好求多了,dp[i][j]表示说在前i场比赛中,连续获胜的次数不超过j次,本题要结合概率计算。


#include <stdio.h>
#include <string.h>
const int N = 505;

int n;
double p, pi[N], dp[N][N];

void solve() {
	pi[0] = 1;
	memset(dp, 0, sizeof(dp));

	for (int i = 0; i <= n; i++) {
		pi[i + 1] = pi[i] * p;
		dp[0][i] = 1;
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			dp[i][j] = dp[i - 1][j];
			if (i == j + 1)
				dp[i][j] -= pi[j + 1];
			else if (i > j + 1)
				dp[i][j] -= dp[i - j - 2][j] * (1 - p) * pi[j + 1];
		}
	}

	double ans = 0;
	for (int i = 1; i <= n; i++)
		ans += (dp[n][i] - dp[n][i - 1]) * i;
	printf("%.6lf\n", ans);
}

int main () {
	while (scanf("%d%lf", &n, &p), n) {
		solve();
	}
	return 0;
}


你可能感兴趣的:(uva 11176 - Winning Streak(期望))