我的解法
def distributeCandies(candies: int, num_people: int):
ans = [0] * num_people
i = 1
while 1:
person = (i % num_people) - 1
ans[person] += i
candies -= i
if (i + i) >= candies:
print("i+1:{} candies:{}".format(i+1, candies))
person = ((i + 1) % num_people) - 1
ans[person] += candies
break
else:
i += 1
return ans
res = distributeCandies(7, 4)
print(res)
最后出来的结果:
i+1:3 candies:4
[1, 2, 4, 0]
def distributeCandies(candies: int, num_people: int):
ans = [0] * num_people
i = 1
while 1:
person = (i % num_people) - 1
ans[person] += i
candies -= i
i += 1
if i >= candies:
print("i:{} candies:{}".format(i, candies))
person = i % num_people - 1
ans[person] += candies
break
return ans
res = distributeCandies(7, 4)
print(res)
1
chenstack 2020-08-09 22:20:03 +08:00
第一段的
if (i + i) >= candies: 应该是打错了,i + 1 打成来 i + i,两个 i 了 |
2
18870715400 OP @chenstack 笔误了, 低级了, 谢谢
|