Jam's balance set 暴力

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");
        }
    }
}

 

转载于:https://www.cnblogs.com/joeylee97/p/7395475.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值