给定一组数,判断是否可以从中选出若干数,使他们的和巧合为k
#include <cstdio> #include <iostream> using namespace std; int a[100],n,k; int dfs(int i,int sum) { if(i==n) return k==sum; if(dfs(i+1,sum)) return 1; if(dfs(i+1,sum+a[i])) return 1; return 0; } int main() { cin>>n; for(int i=0; i<n; i++) cin>>a[i]; cin>>k; if(dfs(0,0)) cout<<"YES"<<endl; else cout<<"NO"; return 0; }