Integer Division (continued)

Source: Internet
Author: User
Tags integer division

The problem of integer division has been mentioned before. In that problem, you only need to find the number of integer division. If you want to output all the division methods of integer division, how can you solve it? For example, for integer 6, the output result is:

6

5 + 1

4 + 2 4 + 1 + 1

3 + 3 3 + 2 + 1 3 + 1

2 + 2 + 2 + 2 + 1 + 1 2 + 1 + 1 + 1 + 1

1 + 1 + 1 + 1 + 1 + 1

We can consider this using a set of thinking. For example, for integer 6, the initial set is equivalent}

From 1 + 1 + 1 + 1 + 1 + 1 to 2 + 1 + 1 + 1 + 1 + 1 is actually equivalent to a pile, take

Add the two 1 s and then put the results back into the set to get the {, 1}. If at this time, I continue to take the two

1 and then put it back to get {2, 2,}. Similarly, if we do the same processing, we will get {2, 2 }.

 

For the third layer, we can take three 1 s from {,} and put them together to get {3, 1,}.

For {3, 1,}, the remaining three ones can have two different strategies.

The first method is to get {3, 3} with three 1 at a time}

The second method is to get {3, 2, 1} with two 1 s}

These are exactly 3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1

Taking the 1 operation from the set can be easily implemented by using the stack. I will take a few to bring up a few and then set the result.

Back in the stack. However, this is not very intuitive. In fact, it will be better to use two stacks. Now we assume there are two

Stacks S1 and S2. press all S1 stacks into 1, and the S2 stack is empty. The number of S1 stack elements is the integer to be decomposed, just like the question

The input is 6, so there are 6 in the S1 stack. Now we need to pop up n 1 s from the S1 stack and add them together.

Press the result into the S2 stack. When the S1 stack is empty, the elements in the S2 Stack are output as results.

 

From the previous analysis, we can divide recursion into two aspects. One is deep recursion, that is, transformation between different layers.

On the other hand, the recursion is the breadth recursion, which divides the large numbers in the same layer into smaller values until they cannot be further divided. For example

6-> 5 + 1-> 4 + 2-> 3 + 3 here is deep Recursion

3 + 3-> 3 + 2 + 1-> 3 + 1 + 1 + 1 here is the breadth Recursion

 

For the elements popped up from the S1 stack, the first element of the first pop-up represents the depth, and the second element of the pop-up is

The number can only be <= the number of elements popped up for the first time. The number of elements popped up for the third time must be <= the number of elements popped up for the second time until the S1 stack is empty.

 

For example

Suppose we input 6, that is, we need to use 6 to break it down. S1 should be empty when there are 6 1 S2 at the beginning.

1. The first time I play 6 1 s, I press 6 into S2. At this time, S1 is empty-> output 6

2. The first time I play 5 1 s, I press 5 into S2. At this time, S1 has 1 s and S2 has 5 S. The next time I pop up the stack, I can only pop up

One 1 is pushed to S2, and now the S1 null S2 is {} and the output is 5 + 1

3. play four 1 s, S2 {4} for the first time. Here, because the number of elements remaining in S1 is greater than 1, different pop-up policies (breadth Recursion

Decomposition)

① Two 1 s are displayed for the second time. S1 null S2 {2, 4} outputs 4 + 2

② 1 is displayed for the second time, and 1 is displayed for the third time. S1 null S2 {, 4} outputs 4 + 1 + 1

4. Three pop-up policies for the first time, S1 {, 1} S2 {3}, are generated because the number of elements remaining in S1 is greater than 1.

① Three 1 s are displayed in the second pop-up. S1 null S2 {3, 3} outputs 3 + 3

② Two S1 {1} S2 {2, 3} are displayed in the second pop-up. A 1 S1 null S2 {1, 2, 3} is displayed, and 3 + 2 + 1 is output.

③ For the second time, a 1, S1 {} S2 {}, 1 S1 {1} S2 {, 3}, and 1 S1 null

S2 {1, 1, 3}

So far

1 S1 {,} S2 {1} is displayed for the first time. 1 S1 {,} S2 {} is displayed.

S1 {, 1} S2 {, 1 }...... finally, S1 null S2 {,} outputs 1 + 1 + 1 + 1 + 1

# Include <stdio. h> # include <stdlib. h> void output (int * a, int top2) // output result {int I; for (I = 0; I <top2-1; I ++) {printf ("% d +", a [I]);} printf ("% d \ n", a [I]);} void partion (INT top1, int top2, int * A) {If (top1 = 0) // if no element exists in S1, the Division is complete and the output is {output (A, top2 );} else {for (INT I = top1; I> = 1; I --) {If (top2 = 0 | I <= A [top2-1]) {A [top2] = I; // The number of 1 Retrieved From S1 is pushed into S2 partion (top1-i, top2 + 1, ); // perform the stack fetch operation again for the remaining 1 in S1 }}} int main (void) {int top1, top2; int *; while (scanf ("% d", & top1) = 1 & top1> = 0) {top2 = 0; A = (int *) malloc (top1 * sizeof (INT); partion (top1, top2, a);} 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.