Python Redundant Combination Problem

Asked 2 years ago, Updated 2 years ago, 23 views

After receiving n, we distribute n balls that are indistinguishable to three abc students, and c should not have fewer balls than b. All branches that can be distributed under these conditions are calculated using overlapping iterations.

n=int(input())
a=0
b=0
c=0

for i in range(1,n+1):
    if b > c:
        break

That's all I know. I wrote the code above, so it might be wrong, but I want to open it because I'm curious about how to solve it.

python

2022-09-20 14:42

1 Answers

We can find a natural solution (a, b, c) that satisfies these two conditions: a+b+c = n, b <= c.

a, b, c all 0...We'll have a value between n, so we'll spin a triple loop to pick out (0, 0, 0) to (n, n, n) that meets the above two conditions.

To reduce the number of loops, turn a double loop, and if the last one is c = n-a-b, the first condition is automatically satisfied, and only the second condition is examined.


2022-09-20 14:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.