1. 程式人生 > >Jam's balance set 暴力

Jam's balance set 暴力

www. sta output div ++ cnblogs amp number input

Jim has a balance and N weights. (1N20)(1≤N≤20)
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.

InputThe first line is a integer T(1T5)T(1≤T≤5), means T test cases.
For each test case :
The first line is

NN, means the number of weights.
The second line are NN number, i‘th number wi(1wi100)wi(1≤wi≤100) means the i‘th weight‘s weight is wiwi.
The third line is a number MM. MM is the weight of the object being measured.
OutputYou should output the "YES"or"NO".Sample Input

1
2
1 4
3
2
4
5

Sample Output

NO
YES
YES


        
 

Hint

For the Case 1:Put the 4 weight alone
For the Case 2:Put the 4 weight and 1 weight on both side
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include
<cmath> #include<map> #include<stack> #include<set> #include<memory> #include<bitset> #include<string> #include<functional> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define MAXN 23 #define INF 0x3f3f3f3f /* 給你一個數字M +-x +-y 能否==M */ int a[MAXN], n, m; set<int> s; int main() { int T, tmp; scanf("%d", &T); while (T--) { s.clear(); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); //int t1, t2; for (int i = 1; i <= n; i++) { set<int>::iterator e = s.end(); vector<int> t; for (set<int>::iterator it = s.begin(); it != s.end(); it++) { if (!s.count(*it + a[i])) t.push_back(*it + a[i]); if (!s.count(abs(*it - a[i]))) t.push_back(abs(*it - a[i])); } s.insert(a[i]); for (int i = 0; i < t.size(); i++) s.insert(t[i]); } scanf("%d", &m); while (m--) { scanf("%d", &tmp); if (s.count(tmp)) printf("YES\n"); else printf("NO\n"); } } }

Jam's balance set 暴力