EOJ.Weights

单点时限: 2.0 sec

内存限制: 512 MB

问题描述

Merchants measured many commodities using weights and a two-pan balance. If you are using a limited set of weights, however, you can only measure certain quantities accurately. For example, suppose that you have only two weights: a 1-ounce weight and a 3-ounce weight. With these you can easily measure out 4 ounces, as shown in Figure1.

It is somewhat more interesting to discover that you can also measure out 2 ounces by shifting the 1-ounce weight to the other side, as shown in Figure2.

Write a program to determine whether it is possible to measure out the desired target amount with a given set of weights.

输入格式

The first line contains an integers n (1≤n≤10) – the size of the given set of weights.

The second line contains n integers (w1,w2,…,wn )(1≤wi≤100)

输出格式

输出格式

Output a binary string s, s.t.:

 Note that s is 1-indexed.

样例:

input:

3
3 5 9

output:

11111111101101001


问题分析:

递归问题,枚举每个砝码放置的位置

   判断  ( 物品重量 + 左边砝码 – 右边砝码 )  ==0 ?

   枚举每个砝码的三种可能(放左边,放右边,不放),

   每个砝码放置后,判断当前称重是否等于0?


 

代码解决部分:

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

typedef long long int lli;
int isPossible(int* w,int n,int target)
{
    int x[]={1,-1,0};
    int r=0,i;
    if(target==0) return 1;
    if(n==0) return 0;
    for(i=0;i<3;i++)
    {
        r=isPossible(w+1,n-1,target+x[i]*w[0]);
        if(r==1) return 1;
    }
    return 0;
}





int main()
{
    int n,w[100];
    int sum=0,target;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",w+i);
        sum+=w[i];
    }
    for(int target=1;target<=sum;target++)
    {
        printf("%d",isPossible(w,n,target));
    }
    system("pause");
    return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NightHacker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值