hdoj-5616-Jam's balance

来源:互联网 发布:发房源赚钱软件 编辑:程序博客网 时间:2024/05/16 14:11

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

题意:给一个只能测量两边重量是否相等的天平,有n个已知重量的砝码,问能否测量出给出的物体的重量。
题解:控制两个标记数组,然后一个个暴力枚举就好了,反正数据范围小

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>using namespace std;int main(){    int t;    int a[105];    bool vis[4005],vis1[4005];    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        for(int i=1;i<=n;i++) scanf("%d",&a[i]);        memset(vis,0,sizeof(vis));        vis[0]=1;        for(int i=1;i<=n;i++)        {            memset(vis1,0,sizeof(vis1));            for(int j=0;j<=2000;j++)            {                if(vis[j]==1)                {                    if(vis1[j-a[i]]>=0) vis1[j-a[i]]=1;                    if(vis1[j+a[i]]<=2000) vis1[j+a[i]]=1;                }            }            for(int i=0;i<=2000;i++)            {                if(vis[i]||vis1[i])                    vis[i]=1;            }        }        int m;        scanf("%d",&m);        while(m--)        {            int x;            scanf("%d",&x);            if(vis[x])                printf("YES\n");            else printf("NO\n");        }    }    return 0;}
0 0
原创粉丝点击