Created
December 15, 2013 05:59
-
-
Save mryoshio/7969465 to your computer and use it in GitHub Desktop.
sonomamma dfs
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> | |
#define MAX_N 20 | |
using namespace std; | |
int a[MAX_N]; | |
int n, k; | |
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() { | |
cout << "n: "; cin >> n; | |
for (int i = 0; i < n; i++) { | |
cout << "a[" << i << "]: "; | |
cin >> a[i]; | |
} | |
cout << "k: "; cin >> k; | |
solve(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment