A recursive splitting numeric Program

Source: Internet
Author: User

/*
Enter an integer, for example, integer 7, which is the sum of N1 + N2 +... + NK = 7, and N1> = N2> =...> = NK (k> = 1 ),
Split it and print out various splits. for 7: 6 + 1 = 7 .. 5 + 2 = 7 .... 1 + 1 + 1 + 1 + 1 + 1 + 1 = 7 There are 14 sharding methods.
Decomposition Process:
Input 3:
(For 5 decomposition) 5 = 4 + 1 5 = 3 + 2
4 = 3 + 1 4 = 2 + 2
(Decomposed by recursion on 3) 3 = 2 + 1
(Decomposed by recursion to 2) 2 = 1 + 1

*/

View code

 1 # Include <iostream>
2 # Include <list>
3
4 Usingnamespace STD;
5
6 Int Count = 0 ; // Total record Decomposition Method
7 Int Temp = 0 ;
8
9 List < Int > Ilist; // Save each decomposition method generated during the decomposition process
10
11 Void Divide ( Int Num, Int Flag );
12 Void Print (list < Int > Ilist, Int Num) // Print each Decomposition Method
13 {
14 List < Int >:: Iterator ibegin, iend;
15 Ibegin = ilist. Begin ();
16 Iend = ilist. End ();
17 If (Ibegin = iend)
18 {
19 Cout < " The list is empty! " <Endl;
20 Exit (exit_failure );
21 }
22 Cout <num < " = " <* Ibegin;
23 Ibegin ++;
24 While (Ibegin! = Iend)
25 {
26 Cout <" + " <* Ibegin;
27 Ibegin ++;
28 }
29 Cout <Endl;
30 }
31
32 Int Main ()
33 {
34 Cout <" Please inpu the num: " <Endl;
35 Int Num;
36 While (CIN> Num, num <= 0 )
37 {
38 Cout < " Enter a positive integer greater than 0! " <Endl;
39 }
40 Temp = num;
41 Divide (Num, 1 );
42 Cout < " Total: " <Count < " Classification! " <Endl;
43 Return0;
44 }
45
46 Void Divide ( Int Num, Int Flag)
47 {
48
49 If (Num = Flag) // When num = flag, the underlying decomposition is reached.
50 {
51 If (! Ilist. Empty ())
52 {
53 Ilist. pop_front ();
54 }
55 Ilist. push_front (FLAG );
56
57 Print (ilist, temp );
58 Ilist. pop_front (); // Delete the first element in the list when a layer is rolled back recursively.
59 Return ;
60 }
61 If (! Ilist. Empty ())
62 {
63 Print (ilist, temp ); // Each time divide is entered, a method is generated to print the output.
64 }
65 For (Int I = flag; I <= num/ 2 ; I ++)
66 {
67 Count ++;
68
69 If (! Ilist. Empty ())
70 {
71 Ilist. pop_front (); // There is a decomposition method for ilist. Front,
72 }
73 Ilist. push_front (I ); // Extract ilist. Front () and push the two decomposition elements to the ilist header.
74 Ilist. push_front (Num-I );
75
76 Divide (Num-I, I ); // Recursion
77 }
78
79 Ilist. pop_front (); // Delete the first element in the list when a layer is rolled back recursively.
80 }

 

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.