我有一个要在网格中显示的n个徽标列表,每行最多3个.什么算法决定每行显示多少,以便每行的徽标数尽可能平衡而不使用超过最小可能行数?
例如:
n -> number in each row
1 -> 1
2 -> 2
3 -> 3
4 -> 2, 2
5 -> 3, 2
6 -> 3, 3
7 -> 3, 2, 2
8 -> 3, 3, 2
9 -> 3, 3, 3
10 -> 3, 3, 2, 2
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 11
你的问题令人困惑,我认为你需要做的是先确定:
number_of_rows = ceil(number_of_logos / 3.0)
Run Code Online (Sandbox Code Playgroud)
然后为每行添加一个徽标,一次一个.
蟒蛇:
import math
def partition_logos(count, lsize):
num_lines = int(math.ceil(count / float(lsize)))
partition = [0] * num_lines
for i in xrange(count):
partition[i%num_lines] += 1
return partition
>>> for i in xrange(1,11):
... print partition_logos(i, 3)
[1]
[2]
[3]
[2, 2]
[3, 2]
[3, 3]
[3, 2, 2]
[3, 3, 2]
[3, 3, 3]
[3, 3, 2, 2]
Run Code Online (Sandbox Code Playgroud)