题目链接:
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1058
1 #include <stdio.h> 2 int n, k, ok, arr[22], vis[22], count; 3 4 void DFS(int pos){ 5 if(count == k) 6 { 7 ok = 1; 8 printf("YES "); 9 for(int i = 0; i < n; ++i) 10 if(vis[i]) 11 printf("%d ", arr[i]); 12 printf(" "); 13 } 14 15 for(int i = pos; i < n; ++i) 16 { 17 count += arr[i]; 18 vis[i] = 1; 19 DFS(i + 1); 20 count -= arr[i]; 21 vis[i] = 0; 22 } 23 } 24 25 int main(){ 26 while(scanf("%d%d", &n, &k) == 2){ 27 ok = 0; 28 for(int i = 0; i < n; ++i){ 29 scanf("%d", arr[i]); 30 vis[i] = 0; 31 } 32 count = 0; DFS(0); 33 if(!ok) printf("NO "); 34 } 35 return 0; 36 }