uva 542 - France '98(概率)
题目大意:有16支球队比赛,给出16支球队的名称,然后给出16*16的表格,g[i][j] 表示i队胜j队的概率,问说16支球队获得总冠军的概率。
解题思路:模拟比赛的过程,将队伍分组,每次和组内的其他队伍决胜负,概率之和即为出现的概率,只要处理好下标就可以了。
#include <stdio.h> #include <string.h> #include <math.h> const int N = 16; char name[N + 10][N + 10]; double p[N + 10][N + 10]; double ans[N + 10], rec[N + 10]; void init() { for (int i = 1; i <= N; i++) scanf("%s", name[i]); int c; for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) { scanf("%d", &c); p[i][j] = c / 100.0; } for (int i = 1; i <= N; i++) ans[i] = 1; } int main () { int group, tmp, g, s; init(); for (int i = 0; i < 4; i++) { group = pow(2, i + 1); tmp = group / 2; for (int i = 1; i <= N; i++) { double c = 0; g = (i - 1) / group; if ((i - 1) % group < tmp) { s = g * group + tmp; } else { s = g * group; } for (int j = 1; j <= tmp; j++) c += ans[i] * p[i][j + s] * ans[j + s]; rec[i] = c; } memcpy(ans, rec, sizeof(ans)); } for (int i = 1; i <= N; i++) printf("%-10s p=%.2lf%c\n", name[i], ans[i] * 100, '%'); return 0; }