注意初始值的给定
#include <iostream> #include <memory.h> #define MaxLen 55 using namespace std; int result1[MaxLen][MaxLen];//result1[i][j]表示将整数i划分为j个数字 int result2[MaxLen][MaxLen];//result2[i][j]表示将整数i划分为不超过j的整数 int result3[MaxLen][MaxLen];//result2[i][j]表示将整数i划分为不超过j的整数 int n,k; void dp() { memset( result1, 0, sizeof(result1) ); for( int i = 1; i <= n; i++) result1[i][1] = 1; for( int i = 1; i <= n; i++) for( int j = 2; j <= i; j++) result1[i][j] = result1[i-j][j] + result1[i-1][j-1]; cout << result1[n][k] <<endl; } void dp2()//最大数不超过k { memset( result2, 0, sizeof(result2) ); result2[0][0] = 1; for( int i = 0; i <= n; i++) for( int j = 1; j <= n; j++) { if( i >= j) { result2[i][j] = result2[i-j][j-1] + result2[i][j-1]; } else { result2[i][j] = result2[i][i]; } } cout << result2[n][n] <<endl; } void dp3() { memset( result3, 0, sizeof(result3) ); for( int i = 0; i <= n; i++ ) { result3[i][1] = 1; if( i%2 ) { result3[0][i] = 1; } } for( int i = 1; i <= n; i++) for( int j = 1; j <= n; j++) { if( j%2 ) { if( i >= j) { result3[i][j] = result3[i-j][j] + result3[i][j-1]; } else result3[i][j] = result3[i][i]; } else result3[i][j] = result3[i][j-1]; } cout << result3[n][n] <<endl; } int main() { while( cin >> n >> k) { dp(); dp2(); dp3(); } return 0; }