Getting all possible sums that add up to a given number

前端 未结 6 1596
灰色年华
灰色年华 2020-12-01 07:17

I\'m making an math app for the android. In one of these fields the user can enter an int (no digits and above 0). The idea is to get all possible sums that make this int, w

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 07:43

    For a number N you know that the max number of terms is N. so, you will start by enumerating all those possibilities.

    For each possible number of terms, there are a number of possibilities. The formula eludes me now, but basically, the idea is to start by (N+1-i + 1 + ... + 1) where i is the number of terms, and to move 1s from left to right, second case would be (N-i + 2 + ... + 1) until you cannot do another move without resulting in an unsorted combination.

    (Also, why did you tagged this android again?)

提交回复
热议问题