标签:
4 13 1 2 4 7
YES 2 4 7
1 #include<stdio.h> 2 #include<stack> 3 using namespace std; 4 int m[21],n,k; 5 stack<int>num; 6 bool dfs(int temp,int i){ 7 if(i==n)return temp==k; 8 if(dfs(temp,i+1))return true; 9 if(dfs(temp+m[i],i+1)){ 10 num.push(m[i]); 11 return true; 12 } 13 return false; 14 } 15 int main(){ 16 while(~scanf("%d%d",&n,&k)){ 17 for(int i=0;i<n;++i)scanf("%d",&m[i]); 18 if(dfs(0,0)){ 19 printf("YES\n"); 20 for(int i=0;!num.empty();i++){ 21 if(i)printf(" "); 22 printf("%d",num.top()); 23 num.pop(); 24 } 25 puts(""); 26 } 27 else printf("NO\n"); 28 } 29 return 0; 30 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4670095.html