枚举子集

一个数学概念:一个集合拥有多个子集
在编程中这里的子集就是多个解,这整个集合里含有多个解 我们需要从这个集合中取一个解

                        Jam's balance


Problem Description   
                       
Jim has a balance and N weights. (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.

input:

The first line is a integer T(1≤T≤5), means T test cases.
For each test case :
The first line is N, means the number of weights.
The second line are N number, i'th number wi(1≤wi≤100) means the i'th weight's     weight is wi.
The third line is a number M. M is the weight of the object being measured.

 Output:
 You 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

题意:给一个天平,t组输入,然后给一个n,代表n个砝码,输入砝码的值,然后m代表m次询问,输入一个x,然后判断能否称出它

题解:将所有情况枚举出来,给n个砝码呢么,它的所有集合个数就为2^n-1
转化为2进制就是(1<

for(int i=0; i<(1<

AC代码:

#include 
using namespace std;
//枚举子集
int a[2005],vis[2005];
int main()
{
   int t,m;
   scanf("%d",&t);
   while(t--)
   {  memset(vis,0,sizeof(vis));
       int n,cur;
       scanf("%d",&n);
       for(int i=0;i=0)
               vis[cur-a[j]]=1;
           }
       }
       scanf("%d",&m);
       while(m--)
       { int x;
           scanf("%d",&x);
           if(vis[x])
            puts("YES");
           else
            puts("NO");
       }
   }
}

你可能感兴趣的:(枚举子集)