Simple integer Partitioning problem total time limit: 100ms memory limit: 65536kB description
A positive integer n is represented as a sum of a series of positive integers, n=n1+n2+...+nk, where n1>=n2>=...>=nk>=1, K>=1.
This representation of positive integer n is called the division of Positive integer n. The number of different partitions of a positive integer n is known as the division of a positive integer n. Input standard input contains several sets of test data. Each set of test data is an integer n (0 < n <= 50). Output for each set of test data, output n partition number. Sample input
5
Sample output
7
Tip 5, 4+1, 3+2, 3+1+1, 2+2+1, 2+1+1+1, 1+1+1+1+1
Thinking: A careful look is not difficult to find this is a recursive problem, the data range is very small, recursive can be, this article using dynamic regulation to solve this problem. Revelation: 1. The data opened just 50 is not enough to know what to think. 2. Small white One such simple problem has not mastered the classification condition. Remember, the problem is to set the boundary conditions in advance, and the rest will fill the array with the same recursion.
The point of the case should be noted, when I<j is not 0, note that the value of J if greater than I, and then increase meaningless, and ways "I" "I" is equal.
#include <iostream>
using namespace std;
unsigned int ways[55][55];
WAYS[I][J] takes the 1~j and is I
int main (void)
{
//freopen ("In.txt", "R", stdin);
Freopen ("OUT.txt", "w", stdout);
int n = 0;
while (CIN >> N)
{for
(int i = 0; I <= N; i++)
{for
(int j = 0; J <= N; j)
{
if (i = = 0)
{
ways[i][j] = 1;
}
if (j = = 0)
{
Ways[i][j] = 0
;
}
}} for (int i = 1; I <= n; i++)
{for
(int j = 1; J <= N; j)
{
if (i >= j)
{
ways[i] [j] = Ways[i-j][j] + ways[i][j-1];
}
else if (i<j)
{
ways[i][j] = Ways[i][i];
}
}} cout << Ways[n][n] << Endl;
}
return 0;
}