DWQA QuestionsCategory: ProgramSum all combinations of n

Given a number n
requirement:
(1) The integer value on the left side of the equation is 1 ~ n-1
(2) Let the sum on the left of the equation be n.

``````If n = 3;
1 + 1 + 1 = 3;
1 + 2 = 3;``````

Is there a requirement for the order on the left?

@zp1996No,

What if it’s four? There is one`2 + 1 + 1`It’s still two

It’s better to search more before asking questions. There are similar questions on segmentfaulthttps://segmentfault.com/q/10&#8230;
One answer is DFS, which can output specific solutions, but the efficiency is low.
One of my answers is dynamic programming (DP), time complexity`O(N^2)`
The generating function mentioned above can also be used, but it is not convenient

If you save each case in an array?

Do you mean that the output you expect is an array, and each element is a string of the form “1 + 1 + 2 = 4”?

Use the integer array instead of the plus sign

You can go to study`Generating function`
This should be the template problem of generating function