Skip to content

Instantly share code, notes, and snippets.

@sat0b
Created May 21, 2017 17:05
Show Gist options
  • Select an option

  • Save sat0b/5703409beb99f6b7f63661a61f5c4520 to your computer and use it in GitHub Desktop.

Select an option

Save sat0b/5703409beb99f6b7f63661a61f5c4520 to your computer and use it in GitHub Desktop.
# 部分和問題 蟻本 p.34
# aからいくつか選び合計がkとなるかを判定
n = 4
a = [1, 2, 4, 7]
k = 13
def dfs(i, sum):
if i == n:
return sum == k
if dfs(i + 1, sum):
return True
if dfs(i + 1, sum + a[i]):
return True
return False
if dfs(0, 0):
print("Yes")
else:
print("No")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment