The division of integers.
For example, a positive integer n = 6 can be classified:
6
5 + 1
4 + 2, 4 + 1 + 1
3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1
2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 1
The problem now is that for a given positive integer N, write an algorithm to print all the partitions.
The user inputs N from the keyboard (range: 1 ~ 10)
The program outputs all the divisions of the integer.
I used recursion for this question, but the idea is different from what I saw on the Internet. It seems simpler,
/**
* 5 = 1 + 1 + 1 + 1 + 1 4 = 1 + 1 + 1 + 1 3 = 1 + 1 + 1 2 = 1 + 1 1 = 1
* 5 = 1 + 1 + 1 + 2 4 = 1 + 1 + 2 3 = 1 + 2 2 = 2
* 5 = 1 + 1 + 3 4 = 1 + 3 3 = 3
* 5 = 1 + 2 + 2 4 = 2 + 2
* 5 = 1 + 4 4 = 4
* 5 = 2 + 3
* 5 = 5
*/
// Observe the integer division above. The Division of N + 1 includes all the division of N + 1, n + 1 = n + 1, and all Division without 1,
// The third part can be obtained from the first part. The numbers first divided are arranged in ascending order, as shown in the preceding figure,
// If the number of elements in a division is greater than 2 and the sum of the first two elements is less than or equal to the third number,
// Merge the first two values of the Division, and the other values remain unchanged to generate a new division.
The Code is as follows:
Public static map getlist (int n ){
Map <string, string> map = new hashmap <string, string> ();
If (n = 1 ){
Map. Put ("1", "1 = ");
Return map;
}
Map = getlist (n-1 );
Map <string, string> MAP2 = new hashmap ();
For (Map. Entry entry: map. entryset ()){
String key = (string) entry. getkey ();
String value = (string) entry. getvalue ();
String STR = iscreate ("1 +" + key );
If (STR! = NULL ){
Map2.put (STR, String. valueof (integer. valueof (value. Replace ("=", "") + 1) + "= ");
}
Map2.put ("1 +" + key, String. valueof (integer. valueof (value. Replace ("=", "") + 1) + "= ");
}
Map2.put ("" + N, N + "= ");
Return MAP2;
}
Public static string iscreate (string s ){
String [] STR = S. Trim (). Split ("\ + ");
If (Str. length> = 3 & (integer. valueof (STR [0]) + integer. valueof (STR [1]) <= integer. valueof (STR [2]) {
Return string. valueof (integer. valueof (STR [0]) + integer. valueof (STR [1]) + S. substring (3 );
}
Return NULL;
}
Public static void main (string [] ARGs ){
System. Out. println ("Enter the integer to divide :");
S = new partition (system. In );
Int n = system. nextint ();
Map <string, string> map = getlist (7 );
Treemap <string, string> treemap = new treemap <string, string> (MAP );
For (Map. Entry entry: treemap. entryset ()){
String key = (string) entry. getkey ();
String value = (string) entry. getvalue ();
System. Out. println (Value + key );
}
}
The result is as follows:
Enter the integer to divide:
7
7 = 1 + 1 + 1 + 1 + 1 + 1 + 1
7 = 1 + 1 + 1 + 1 + 1 + 2
7 = 1 + 1 + 1 + 1 + 3
7 = 1 + 1 + 1 + 2 + 2
7 = 1 + 1 + 1 + 4
7 = 1 + 1 + 2 + 3
7 = 1 + 1 + 5
7 = 1 + 2 + 2 + 2
7 = 1 + 2 + 4
7 = 1 + 3 + 3
7 = 1 + 6
7 = 2 + 2 + 3
7 = 2 + 5
7 = 3 + 4
7 = 7
Newcomers, poor code writing. Sorry!