python通俗易懂的题解 | #数组分组#

数组分组

https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86

length = int(input())
num_list = list(map(int, input().split()))

five_list = []
three_list = []
not_five_or_three_list = []
for i in num_list:
    if i % 5 == 0:
        five_list.append(i)
    elif i % 3 == 0:
        three_list.append(i)
    else:
        not_five_or_three_list.append(i)
# print(f"{five_list=}")
# print(f"{three_list=}")
# print(f"{not_five_or_three_list=}")
sum_five = sum(five_list)
sum_three = sum(three_list)
sum_not_five_or_three = sum(not_five_or_three_list)


def recu(not_five_or_three_list, diff):
    if len(not_five_or_three_list) == 0 and diff == 0:
        return True
    else:
        for i in range(len(not_five_or_three_list)):
            if recu(
                not_five_or_three_list[:i] + not_five_or_three_list[i + 1 :],
                diff + not_five_or_three_list[i],
            ):
                return True
            elif recu(
                not_five_or_three_list[:i] + not_five_or_three_list[i + 1 :],
                diff - not_five_or_three_list[i],
            ):
                return True
            else:
                return False


result = recu(not_five_or_three_list, sum_five - sum_three)
if result:
    print("true")
else:
    print("false")

全部评论

相关推荐

Cassifa:发的字比你都多的一律视为骗子或者想白嫖压榨实习生的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务