Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created May 4, 2017 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/f217a9d8cade97094962827a1fe4a338 to your computer and use it in GitHub Desktop.
Save whatalnk/f217a9d8cade97094962827a1fe4a338 to your computer and use it in GitHub Desktop.
アリ本 2-1 p34 部分和問題
#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