《算法竞赛-训练指南》-第二章-2.16_UVa 11427

来源:互联网 发布:简单网络架构图 编辑:程序博客网 时间:2024/05/17 04:44

这道不错不错,给了一种方法来解决,两人博弈中,通过局数来判断输赢的题目,以前得分很多情况来判断。现在可以用dp来求出来所有的条件了。确实不错。


首先把dp的概念一定要弄清楚,d[i][j]代表的含义是在i的总局数中,j局获胜,但是最终还是没有达到条件的情况。那么符合条件的情况就是,1减去所有的这种不符合的条件的概率和。解决了这个其余的都很好做。哪怕不优化后面的问题我觉得都能过了这个问题。

还是要多看看这道题目,记住这种解法!

有首好歌:好胆你就来!!

贴出代码:

#include <stdio.h>#include <string.h>#include <iostream>#include <string>using namespace std;const int MAXN = 100 + 11;double d[MAXN][MAXN]; //d[i][j]表示在i局中,j局胜利,但是还是没有满足条件,  //去睡觉了 int main(){int T;int a1, a2;int n;scanf("%d", &T);for (int Case = 1; Case <= T; Case++){scanf("%d/%d%d", &a1, &a2, &n);double p = double(a1) / a2;memset(d, 0, sizeof(d));d[0][0] = 1;d[0][1] = 0;for (int i = 1; i <= n; i++){for (int j = 0; j * a2 <= a1 * i; j++){d[i][j] = d[i - 1][j] * (1 - p);if (j){d[i][j] += d[i - 1][j - 1] * p;}}}double ans = 0;for (int i = 0; i * a2 <= a1 * n; i++){ans += d[n][i];}printf("Case #%d: %d\n", Case, (int)(1 / ans));}//system("pause");return 0;}


原创粉丝点击