杭电 hdu 5616 Jam's balance【思维】【暴力枚举】


Jam's balance

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 188    Accepted Submission(s): 82


Problem Description
Jim has a balance and N weights.  (1N20)
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(1T5) , 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(1wi100)  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
 

Source

因为N不大,并且wi也不大,我们这里可以应用数组标记的方法,暴力枚举出每一种可能。

首先我们需要两个vis数组 。我们这里规定1代表有这个可能,0代表没有这个可能、这里对应核心代码详解:
并且这里要注意,题干中样例如果加一个3的话,也是应该输出YES的~

        memset(vis1,0,sizeof(vis1));
        memset(vis2,0,sizeof(vis2));
        vis1[0]=1;//0是一定可以称出来的
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=2000;j++)//20*100是最大的边界
            {
                if(vis1[j]!=0)//如果vis1数组上标记过这个数。
                {
                    vis2[j]=1;
                    vis2[j+a[i]]=1;//处理+
                    vis2[abs(j-a[i])]=1;//处理-
                }
            }
            for(int k=0;k<=2000;k++)//数组赋值过去
            {
                vis1[k]=vis2[k];
                vis2[k]=0;
            }
        }


蓝后是完整的AC代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>

using namespace std;
int vis1[2005];
int vis2[2005];
int a[2005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        memset(vis1,0,sizeof(vis1));
        memset(vis2,0,sizeof(vis2));
        vis1[0]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=2000;j++)
            {
                if(vis1[j]!=0)
                {
                    vis2[j]=1;
                    vis2[j+a[i]]=1;
                    vis2[abs(j-a[i])]=1;
                }
            }
            for(int k=0;k<=2000;k++)
            {
                vis1[k]=vis2[k];
                vis2[k]=0;
            }
        }
        int m;
        scanf("%d",&m);
        while(m--)
        {
            long long int q;
            scanf("%I64d",&q);
            if(vis1[q]==0||q<0||q>2000)
            {
                printf("NO\n");
            }
            else
            {
                printf("YES\n");
            }
        }
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值