Algorithm Design: Integer Division

Source: Internet
Author: User

// Represents a positive integer N as the sum of a series of positive integers,
// N = N1 + N2 +... + NK (wherein, N1> = N2> =...> = NK, k> = 1)
// This expression of positive integer N is called a division of positive integer n.
// Different numbers of positive integer N are called the numbers of positive integer n.

// Calculates the number of partitions.

// Calculate the number of partitions whose maximum N1 value is not greater than m as Q (n, m ).
// The recursive relationship is as follows:
// 1. Q (n, 1) = 1, n> = 1;
// 2. Q (n, m) = Q (n, n), m> = N;
// 3. Q (n, n) = 1 + q (n, n-1 );
// 4. Q (n, m) = Q (m-1) + q (n-M, m), N> m> 1;

////////////////////////////////////////
# Include "iostream. H"

Int Q (int n, int m)
{
If (n <1 | M <1)
Return 0;
If (n = 1 | M = 1)
Return 1;
If (n <m)
Return Q (N, N );
If (n = m)
Return Q (n, m-1) + 1;
Return Q (n, m-1) + q (n-M, M );
}
Void main ()
{
Cout <q (6, 6) <Endl;
}

////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////

// Division
//
# Include "iostream. H"
# Include "iomanip. H"

# Deprecision Max 1024

Void print (int * map, int Len)
{
Static int Total = 1;
Cout <"division" <SETW (4) <total ++ <":";
For (INT I = 0; I <Len; I ++)
Cout <SETW (5) <map [I];
Cout <Endl;
}

Int P (int n, int M, int * map, int Len)
{
If (n> = 1 & M = 1)
{
// Flag1:
// When M = 1, there is only one method. n = 1 + 1 +...
// Work with flag2 to complete the output of such Decomposition
Map [Len] = 1;
P (n-1, m, MAP, Len + 1 );
Return 1;
}
Else if (n = 0 & M = 1)
{
// Flag2:
// Use flag1 to complete division M = 1
Print (MAP, Len );
Return 1;
}
Else if (n = 1 & M> 1)
{
// Flag3:
// When n = 1, the decomposition is completed and the output is processed.
Map [Len] = N;
Print (MAP, Len + 1 );
Return 1;
}
Else if (n <m)
{
// Flag4:
// Because of the location, m, n in this case and below are all> 1
Return P (n, n, MAP, Len );
}
Else if (n = m)
{
// Flag5:
// In this case, if the map bit is assigned m, a division can be completed.
Map [Len] = m;
Print (MAP, Len + 1 );
// Continue processing
Return P (n, m-1, MAP, Len) + 1;
}
Else
{
// There are two solutions
// Method 1:
// The current map bit is assigned to M, processing P (n-M, m)
Map [Len] = m;
Int S1 = P (n-M, M, MAP, Len + 1 );
// Method 2:
Int S2 = P (n, m-1, MAP, Len );
Return S1 + S2;
}

}
Void main ()
{
Int map [Max] = {0 };
Int Len = 0;

Cout <"Total =" <p (6, 6, MAP, Len) <Endl;
}
// Program 2 is an extension of program 1. The basic idea is similar to that of program 1. On the basis of understanding program 1,
// You can understand Program 2 very well

////////////////////////////////////////
// References:
// Computer algorithm design and analysis Press

 

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.