当前位置:网站首页>[niuke.com] ky41 put apples

[niuke.com] ky41 put apples

2022-06-11 21:47:00 When the flower does not wither

 Insert picture description here
Use dynamic programming to solve .

When m >= n when , Or there is at least one apple on each plate , Or at least one empty plate , therefore dp[ i ][ j ] = dp[ i ][ j - 1 ] + dp[ i - j ][ j ].
When m < n when , At least n - m A plate is empty , At most m There are apples on a plate , therefore dp[ i ][ j ] = dp[ i ][ i ].
The boundary conditions , When there is only one plate or no apples , There is only one way .

#include<iostream>
#include<cstdio>

using namespace std;

const int N = 10 + 10;

int dp[N][N];

int main(){
    
    int m, n;
    while(scanf("%d%d", &m, &n) != EOF){
    
        for(int i = 0; i <= m; i++) dp[i][1] = 1;
        for(int j = 0; j <= n; j++) dp[0][j] = 1;
        for(int i = 1; i <= m; i++){
    
            for(int j = 1; j <= n; j++){
    
                if(j <= i) dp[i][j] = dp[i][j - 1] + dp[i - j][j];
                else dp[i][j] = dp[i][i];
            }
        }
        printf("%d\n", dp[m][n]);
    }
    return 0;
}
原网站

版权声明
本文为[When the flower does not wither]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011652457624.html