Created
May 4, 2017 23:34
-
-
Save whatalnk/f217a9d8cade97094962827a1fe4a338 to your computer and use it in GitHub Desktop.
アリ本 2-1 p34 部分和問題
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
const int MAX_N = 20; | |
int n, k; | |
int a[MAX_N]; | |
bool dfs(int i, int 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; | |
} | |
void solve() | |
{ | |
if (dfs(0, 0)) | |
cout << "Yes" << endl; | |
else | |
cout << "No" << endl; | |
} | |
int main() | |
{ | |
ios::sync_with_stdio(false); | |
cin >> n; | |
for (int i = 0; i < n; i++) | |
{ | |
cin >> a[i]; | |
} | |
cin >> k; | |
solve(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment