其他分享
首页 > 其他分享> > 牛客华为机试HJ93

牛客华为机试HJ93

作者:互联网

原题传送门

1. 题目描述

2. Solution

DFS

import sys

if sys.platform != "linux":
    sys.stdin = open("input/HJ93.txt")


def dfs(three, five, other):
    if not other:
        return sum(three) == sum(five)
    if dfs(three + other[:1], five, other[1:]):
        return True
    if dfs(three, five + other[:1], other[1:]):
        return True


def solve(n, nums):
    three, five, other = [], [], []
    for x in nums[::-1]:
        if x % 3 == 0:
            three.append(x)
        elif x % 5 == 0:
            five.append(x)
        else:
            other.append(x)
    if dfs(three, five, other):
        print("true")
    else:
        print("false")


while True:
    try:
        n = int(input().strip())
        nums = list(map(int, input().strip().split()))
        solve(n, nums)
    except:
        break

标签:nums,three,dfs,牛客,other,five,机试,HJ93,append
来源: https://www.cnblogs.com/junstat/p/16177316.html