标签:nyoj 1058
4 13 1 2 4 7
YES 2 4 7
#include <stdio.h> int n, k, ok, arr[22], vis[22], count; void DFS(int pos){ if(count >= k){ if(count == k){ if(!ok){ ok = 1; printf("YES\n"); } for(int i = 0; i < n; ++i) if(vis[i]) printf("%d ", arr[i]); printf("\n"); } return; } for(int i = pos; i < n; ++i){ count += arr[i]; vis[i] = 1; DFS(i + 1); count -= arr[i]; vis[i] = 0; } } int main(){ while(scanf("%d%d", &n, &k) == 2){ ok = 0; for(int i = 0; i < n; ++i){ scanf("%d", arr + i); vis[i] = 0; } count = 0; DFS(0); if(!ok) printf("NO\n"); } return 0; }
NYOJ 1058 部分和问题 【DFS】,布布扣,bubuko.com
标签:nyoj 1058
原文地址:http://blog.csdn.net/chang_mu/article/details/30847055