首页 > 代码库 > POJ2151 动态规划
POJ2151 动态规划
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 int m, t, n; 8 double dp[1005][40][40]; 9 double p[1005][40];10 double p1, p2;11 12 int main()13 {14 while (scanf("%d%d%d", &m, &t, &n) && m) { //这里的&&写成了&,wa了1个小时15 memset(dp, 0, sizeof(dp));16 for (int i = 1; i <= t; i++) {17 for (int j = 1; j <= m; j++) 18 scanf("%lf", &p[i][j]);19 }20 21 for (int i = 1; i <= t; i++) {22 23 dp[i][0][0] = 1.0;24 for (int j = 1; j <= m; j++) {25 dp[i][j][0] = dp[i][j - 1][0] * (1 - p[i][j]);26 27 for (int l = 1; l <= j; l++) {28 dp[i][j][l] = dp[i][j - 1][l] * (1 - p[i][j]) 29 + dp[i][j - 1][l - 1] * p[i][j]; 30 }31 }32 }33 34 double s1 = 1;35 double s2 = 1;36 37 for (int i = 1; i <= t; i++) {38 p1 = 0;39 for (int j = 1; j < n; j++) 40 p1 += dp[i][m][j];41 s1 *= p1;42 }43 44 45 for (int i = 1; i <= t; i++) {46 p2 = 1 - dp[i][m][0];47 s2 *= p2;48 }49 printf("%.3lf\n", s2 - s1);50 }51 return 0;52 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。