叠积木
标题:叠积木 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
有一堆长方体积木,它们的宽度和高度都相同,但长度不一。小橙想把这堆积木叠成一面墙,墙的每层可以放一个积木,也可以将两个积木拼接起来,要求每层的长度相同。若必须用完这些积木,叠成的墙最多为多少层?
from collections import Counter def can(max_num, num_count): ret = True for i in num_count: if 0 < num_count[i] and i != max_num: pair_num = max_num -i if pair_num == i: if num_count[i]%2 != 0: ret = False break elif num_count[pair_num] != num_count[i]: ret = False break return ret while True: try: nums = list(map(int, input().split())) total_sum, max_num = sum(nums), max(nums) num_count = Counter(nums) max_height = -1 for i in range(total_sum // max_num, 0, -1): if total_sum % i == 0: num = total_sum // i if num <= max_num * 2: if can(total_sum // i, num_count) and max_height < i: max_height = i print(max_height) except: break