zoukankan      html  css  js  c++  java
  • 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
    ");
                else
                    printf("NO
    ");
            }
        }
    }
  • 相关阅读:
    cesium 学习(七) HeadingPitchRoll
    cesium 学习(六) 坐标转换
    cesium 学习(五) 加载场景模型
    Cesium 学习(一)环境搭建
    Cesium 学习(二)所支持的模型数据类型,以及转换
    cesium 学习(四) Hello World
    Cesium 学习(三)各种资源链接
    【Windows编程】系列第十一篇:多文档界面框架
    【Windows编程】系列第十篇:文本插入符
    【Windows编程】系列第八篇:通用对话框
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7395475.html
Copyright © 2011-2022 走看看