Addition Decomposition Scheme

Source: Internet
Author: User
The problem is described as a positive integer N, and the decomposition method of all its positive integer addition is output.

Note:

1. Determine whether the location of the switching addition is considered as a different Decomposition Scheme Based on the input requirements.

2. Non-decomposition is also considered as a decomposition solution.

3. Output all decomposition schemes in Lexicographic Order. For the format, see the example.
The input format contains one row, which contains two positive integers, N and M, separated by a space. N indicates the positive integer to be decomposed. m is 1 or 2:

1 indicates whether the location of the switching addition is considered as a different Decomposition Scheme;

2 indicates whether the location of the switching addition is regarded as the same Decomposition Scheme.
The output format outputs several rows. Each row represents a Decomposition Scheme. For a scheme, n is output first, and then "=" is output ". Then, the number of splits is output. Different numbers are connected by a "+.
Example input 5 2
Sample output 5 = 1 + 1 + 1 + 1 + 1

5 = 1 + 1 + 1 + 2

5 = 1 + 1 + 3

5 = 1 + 2 + 2

5 = 1 + 4

5 = 2 + 3

5 = 5
Input and Output Example 2 example input 5 1
Sample output 5 = 1 + 1 + 1 + 1 + 1

5 = 1 + 1 + 1 + 2

5 = 1 + 1 + 2 + 1

5 = 1 + 1 + 3

5 = 1 + 2 + 1 + 1

5 = 1 + 2 + 2

5 = 1 + 3 + 1

5 = 1 + 4

5 = 2 + 1 + 1 + 1

5 = 2 + 1 + 2

5 = 2 + 2 + 1

5 = 2 + 3

5 = 3 + 1 + 1

5 = 3 + 2

5 = 4 + 1

5 = 5

Answer: BFS
# Include <iostream> using namespace STD; int sum = 0;/** M = 1. When the position of the switching addition is regarded as different decomposition schemes, count = 0 */void dfs1 (int n, int count, int * P) {If (n = 0) {// recursive base, result cout <sum <"="; for (INT I = 0; I <count; I ++) if (I = count-1) cout <p [I] <Endl; elsecout <p [I] <"+"; return ;}for (INT I = 1; I <= N; I ++) {P [count] = I; dfs1 (n-I, Count + 1, P);} return ;}/ * m = 2, when the position of the switching addition is regarded as the same Decomposition Scheme, Count = 0 */void dfs2 (int n, int count, int * P) {If (n = 0) {// recursive base, output to the leaf node Result cout <sum <"="; for (INT I = 0; I <count; I ++) if (I = count-1) cout <p [I] <Endl; elsecout <p [I] <"+"; return;} int K = count> 0? P [count-1]: 1; // The number in the array should be in a non-descending order for (INT I = K; I <= N; I ++) {P [count] = I; dfs2 (n-I, Count + 1, p) ;}} int main () {int n, m; cin> N> m; sum = N; int * P = new int [N]; If (M = 1) dfs1 (n, 0, P ); else if (M = 2) dfs2 (n, 0, P); 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.