今日心得
- 对于大数组的定义要放在main函数外,否则会报错
- 认真思考动态规划的边界设置,不是机械地设置1或0
动态转移方程:
dp[i][j] = dp[i-1][j]*j/n + dp[i-1][j-1]*(n-j+1)/n;
参考代码
#include<bits/stdc++.h>
using namespace std;int main()
{int n, m;scanf("%d %d", &n, &m);if(n > m){printf("0.0000");}else{double dp[21][21] = {0};for(int i = 1; i <= m; i++){dp[i][1] = pow(1.0/n, i-1);}for(int i = 2; i <= m; i++){for(int j = 1; j <= n; j++){dp[i][j] = dp[i-1][j]*j/n + dp[i-1][j-1]*(n-j+1)/n;}}printf("%.4f", dp[m][n]); }return 0;
}
参考代码
#include<bits/stdc++.h>
using namespace std;
int dp[1005][1005] = {0};int main()
{int n;scanf("%d", &n);for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){scanf("%d", &dp[i][j]);}}for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){dp[i][j] += max(dp[i-1][j], dp[i][j-1]);}}printf("%d", dp[n][n]);return 0;
}