注意事项:
天枰左右都能放
参考代码:
#include<bits/stdc++.h> using namespace std; int n, m, f[30], w[100]; bool ser; void dfs(int a, int t, int b) { if (ser) return; if (a == b) { ser = true; return; } if (t > n) return; dfs(a + f[t],t+1, b); dfs(a, t+1, b); dfs(a, t+1, b + f[t]); } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> f[i]; for (int i = 1; i <= m; i++) cin >> w[i]; for (int i = 1; i <= m; i++) { ser = false; dfs(0,1,w[i]); if (ser) cout << "YES" << endl; else cout << "NO" << endl; } }
0.0分
11 人评分