Generating all distinct partitions of a number

后端 未结 5 1921
北海茫月
北海茫月 2020-12-14 12:56

I am trying to write a C code to generate all possible partitions (into 2 or more parts) with distinct elements of a given number. The sum of all the numbers of a g

5条回答
  •  情话喂你
    2020-12-14 13:41

    What you're trying to do doesn't make a lot of sense to me but here's how I would approach it.

    First, I'd create a loop that iterates i from 1 to n - 1. In the first loop, you could add the partition 1, i. Then I'd go recursive using the value in i to get all the sub-partitions that can also be added to 1.

    And then continue to 2, and so on.

提交回复
热议问题