标签:style blog http color strong os
4 13 1 2 4 7
YES 2 4 7
解题:一顿乱搜。。。。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 using namespace std; 11 int n,d[21],ans[21],m,res; 12 bool dfs(int cur,int add,int tot) { 13 if(cur >= n) { 14 if(add == res) { 15 m = tot; 16 return true; 17 } 18 return false; 19 } 20 if(dfs(cur+1,add,tot)) { 21 return true; 22 } 23 if(dfs(cur+1,add+d[cur],tot+1)) { 24 ans[tot] = d[cur]; 25 return true; 26 } 27 return false; 28 } 29 int main() { 30 while(~scanf("%d %d",&n,&res)) { 31 int i; 32 for(int i = 0; i < n; i++) 33 scanf("%d",d+i); 34 if(dfs(0,0,0)) { 35 puts("YES"); 36 for(i = 0; i+1 < m; i++) 37 printf("%d ",ans[i]); 38 printf("%d\n",ans[i]); 39 } else puts("NO"); 40 } 41 return 0; 42 }
NYOJ 1058 部分和问题,布布扣,bubuko.com
标签:style blog http color strong os
原文地址:http://www.cnblogs.com/crackpotisback/p/3852191.html