ZJU2759 Perfect Weighing Skill

 

Perfect Weighing Skill

Time limit: 10 Seconds   Memory limit: 32768K  
Total Submit: 863   Accepted Submit: 386  

Michael is interested in physics. During his playing with balances and counterpoises, he finds that if he chooses a set of suitable counterpoises, he can weigh many more different weights than others. After a series of hard work, Michael decides to choose the counterpoises which have the weight 1,3,9,27,....,exactly all the power exponents of 3. He thinks he can weigh every weight with these counterpoises. But Alex is doubt about this. He says he can give an object which cannot be weighed by this set of counterpoises. Now Michael is asking for your help to win the bet. He also tells you a secret to help: "The counterpoises could be placed on both sides of the balance. As well, the weight of all Alex's objects are no more than integers. Cheer up! You'll win."

Input
The input consists of multiple test cases. Each case contains a single integer M, 1<=M<=500000000, which represents the weight of Alex's object to be weighed by the balance.

Output
For each test case, output 2 lines. The first line is the counterpoises which should be put at the same side of the object. If there are none, output -1.The second line is the counterpoises which should be put at the different side of the object. If there are none, output -1.If the object cannot be weight, you should output -1 only in both line. An empty line should be printed after each test case.
Sample Input
13
35
Sample Output

-1
1 3 9

1
9 27

题目描述:
一天平的砝码质量全是3的n次方,即1,3,9,27, ……求一种测量方案称出质量为M的物品(m<=500000000)。砝码可以放在天平的两侧。
分析:
如果数据量不大,可以使用搜索的方法将所有的测量方法事先计算出来,然后打表。

此题物品质量很大。所以搜索走不通。那该如何来确定砝码的放法呢?

可以考虑把物品质量用三进制表示:
质量M  三进制表示的M    砝码(0表示不放,-1表示与物品同侧,1表示与物品异测)
  1        0 0 1              0  0  1
  2        0 0 2              0  1 -1
  3        0 1 0              0  1  0
  4        0 1 1              0  1  1
  5        0 1 2              1 -1 -1
  6        0 2 0              1 -1  0
  7        0 2 1              1  -1  1
  8        0 2 2              1  0  -1
  从该表我们看出以下规律:
  对于三进制的某位s[i]
  当s[i]=0时,什么都不要做。
  s[i]=2时,放一个砝码与物品同侧,然后s[i+1]进行加1操作.
  s[i]=1时,砝码放在物品异侧。
代码:

#include<stdio.h>
#define N 20

void printa(int a[],int n){
 int i;
 for(i=0;i<n;i++){
  if(i)printf(" ");
  printf("%d",a[i]);
 }
 printf("/n");
}

int main(){

 int i,j,p,m,r;
 int a[N],b[N],s[N];
 s[0]=1;
 for(i=1;i<N;i++)
  s[i]=s[i-1]*3;

 while(scanf("%d",&m)!=EOF){
  i=j=p=0;
  while(m){
   r=m%3;
   m=m/3;
   if(r==2){
    a[i++]=s[p];
    m=m+1;
   }else if(r==1){
    b[j++]=s[p];
   }
   p++;
  }


  if(!i)printf("-1/n");
  else printa(a,i);
  if(!j)printf("-1/n");
  else printa(b,j);
  printf("/n");
 }
 return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值