HDU1028
从王晓东的书中学到了对整数的划分问题:
在正整数n的所有不同划分中,将最大加数n1不大于m的划分个数记为q(n,m)。可以建立q(n,m)的如下递归关系:
<1>q(n,m) = 1, n >= 1
当最大加数n1不大于1时,任何正整数n只有一种划分形式,n = 1 + 1 + 1 +...+ 1
<2>q(n,m) = q(n,n), m >= n
最大加数n1实际上不能大于n
<3>q(n,n) = 1 + q(n,n - 1)
正整数n的划分由n1 = n的划分和n1 < n - 1的划分组成
<4>q(n,m) = q(n, m - 1) + q(n - m, m),n > m > 1
正整数n的最大加数n1不大于m的划分由n1 = m的划分和n1 <= m - 1的划分组成
#include <stdio.h>
#define N 124
int Fen[N][N] = {0};
void Calc()
{
int i,j;
for(i = 1; i <= 120; i++)
for(j = 1; j <= 120; j++)
if(i < 1 || j < 1)
Fen[i][j] = 0;
else if(i == 1 || j == 1)
Fen[i][j] = 1;
else if(i < j)
Fen[i][j] = Fen[i][i];
else if(i == j)
Fen[i][j] = Fen[i][j - 1] + 1;
else Fen[i][j] = Fen[i][j - 1] + Fen[i - j][j];
}
int main()
{
int n;
Calc();
while(scanf("%d",&n) != EOF)
printf("%d\n",Fen[n][n]);
return 0;
}
#define N 124
int Fen[N][N] = {0};
void Calc()
{
int i,j;
for(i = 1; i <= 120; i++)
for(j = 1; j <= 120; j++)
if(i < 1 || j < 1)
Fen[i][j] = 0;
else if(i == 1 || j == 1)
Fen[i][j] = 1;
else if(i < j)
Fen[i][j] = Fen[i][i];
else if(i == j)
Fen[i][j] = Fen[i][j - 1] + 1;
else Fen[i][j] = Fen[i][j - 1] + Fen[i - j][j];
}
int main()
{
int n;
Calc();
while(scanf("%d",&n) != EOF)
printf("%d\n",Fen[n][n]);
return 0;
}