P153:子集和问题

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int n,s;
 6 bool vis[100];
 7 int a[100];
 8 bool calc(int cur) {
 9     if(cur==s) {
10         for(int i=0;i<n;i++) {
11             if(vis[i])
12                 printf("%d ",a[i]);
13         }
14         puts("");
15         return true;
16     }
17     else {
18         for(int i=0;i<n;i++) {
19             if(!vis[i]) {
20                 vis[i] = true;
21                 if(calc(cur+a[i]))
22                     return true;
23                 vis[i] = false;
24             }
25         }
26     }
27     return false;
28 }
29 
30 int main()
31 {
32     freopen("in.txt","r",stdin);
33     scanf("%d%d",&n,&s);
34     for(int i=0;i<n;i++) {
35         scanf("%d",&a[i]);
36     }
37     if(!calc(0))
38         puts("No Solution!");
39     return 0;
40 }
View Code

相关文章: