题解 | #数组分组#

数组分组

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

import re
import sys

n = int(input())

a = list(map(int, input().split()))

def check(list1,list2,left):
    if len(left)==0:
        sum1 = sum(list1)
        sum2 = sum(list2)
        if sum1==sum2:
            return True
        else:
            return False
    else:
        this = left[0]
        leftnext = left[1:]
        if this%5==0:
            list1 = list1+[this]
            res = check(list1,list2,leftnext)
        elif this%3==0:
            list2 = list2+[this]
            res = check(list1,list2,leftnext)
        else:
            list1new = list1+[this]
            res1 = check(list1new,list2,leftnext)
            list2new = list2+[this]
            res2 = check(list1,list2new,leftnext)

            res = res1 or res2
        
        return res

res = check([],[],left=a)
if res:
    print('true')
else:
    print('false')

全部评论

相关推荐

10-09 22:05
666 C++
找到工作就狠狠玩CSGO:报联合国演讲,报电子烟设计与制造
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务