【发布时间】:2013-03-30 18:35:19
【问题描述】:
我们正在玩一个游戏,其中有 n 个孩子围成一圈坐着。他们每个人都有一些巧克力。巧克力的总数使得它们可以平均分配给所有孩子。
在一轮中,任何一个孩子向左或向右传递一颗巧克力。我们需要回答,至少需要多少轮这样的轮次才能让所有人拥有相同数量的巧克力。
给出了孩子的数量 n 和每个孩子的巧克力数量。
我们应该应用什么算法??
【问题讨论】:
我们正在玩一个游戏,其中有 n 个孩子围成一圈坐着。他们每个人都有一些巧克力。巧克力的总数使得它们可以平均分配给所有孩子。
在一轮中,任何一个孩子向左或向右传递一颗巧克力。我们需要回答,至少需要多少轮这样的轮次才能让所有人拥有相同数量的巧克力。
给出了孩子的数量 n 和每个孩子的巧克力数量。
我们应该应用什么算法??
【问题讨论】:
我的启发是:
while (not equally divided)
find kid A with the most chocolates
while (A has more that he should)
find closest kid B with fewer than he should
pass one from A to B and add to the number of moves (taking distance into account)
【讨论】: