DWQA QuestionsCategory: ProgramSum all combinations of n
bbaimu asked 2 months ago

Given a number n
(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


zp1996 replied 2 months ago

What if it’s four? There is one2 + 1 + 1It’s still two

2 Answers
Dappur answered 2 months ago

It’s better to search more before asking questions. There are similar questions on segmentfaulthttps://segmentfault.com/q/10…
One answer is DFS, which can output specific solutions, but the efficiency is low.
One of my answers is dynamic programming (DP), time complexityO(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

fish answered 2 months ago

You can go to studyGenerating function
This should be the template problem of generating function