Mooc Simple Integer Partitioning problem

Source: Internet
Author: User
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;
}
  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.