51nod 1268 和为K的组合

来源:互联网 发布:百度关键词优化多少钱 编辑:程序博客网 时间:2024/05/16 15:39

数据量比较小,dfs暴力水过

#include <bits/stdc++.h>using namespace std;int n,k;int A[25];bool correct = false;void dfs(int cnt,int sum){    if(sum == k)    {        correct = true;        return;    }    if(correct) return;    if(cnt == n) return;    dfs(cnt+1,sum+A[cnt]);    dfs(cnt+1,sum);}int main(){    ios::sync_with_stdio(false);    cin >> n >> k;    for(int i = 0; i < n; ++i)        cin >> A[i];    dfs(0,0);    if(correct) cout << "Yes" << endl;    else cout << "No" << endl;    return 0;}