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;``````
zp1996 replied 2 months ago

Is there a requirement for the order on the left?

zp1996 replied 2 months ago

@zp1996No,

zp1996 replied 2 months ago

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

bbaimu replied 2 months ago

If you save each case in an array?

Dappur replied 2 months ago

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

bbaimu replied 2 months ago

Use the integer array instead of the plus sign

You can go to study`Generating function`