hdu3029


Scales

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 512    Accepted Submission(s): 230


Problem Description
Give you a scale, a goods weigts m kilograms. And then give you some stones weighting 1, 3, 9, 27, ..., 3^k. Of course, the number of different weights' is only ONE.

Now put the goods on the left of scale. And then you should put some stones on two sides of scale to make the scale balanced.
 

Input
An integer m, stands for the weights of the goods (0 ≤ m ≤ 100 000 000)
 

Output
You should output two lines.

In the first line, the first integer N1 is the number of stones putting on the left of scale, and then N1 integers follow(in the ascend order), indicating the weight of stones putting on the left, seperated by one space. In the second line, please use the same way as the first line's to output the N2, which is the number of stones putting on the right side. Then following N2 integers, which are in ascending order, indicate the stones' weight putting on the right side.
 

Sample Input
  
  
42 30
 

Sample Output
  
  
3 3 9 27 1 81 0 2 3 27
 

Source
 

Recommend
gaojie



题意:

给你一个天平,想办法用重量为3的k次方的一堆砝码,称出重m千克的物品

思路:

将m转化成一个三进制数字a,然后转换成两个三进制数字a与b相加得到c,保证b,c中只有0和1,。

得到的b,c中含有1的位置即是选用当前的砝码。



#include<cstdio>

#include<cstring>
#include<vector>
using namespace std;




const int N=1000;
int m;
int a[N+7];
int b[N+7];
int c[N+7];
int main()
{
    while(scanf("%d",&m)!=EOF)
    {
        int len=0;


        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        int tmp=m;
        while(tmp)
        {
            a[len++]=tmp%3;
            tmp/=3;
        }


        int coun=0;
        for(int i=0;i<len;i++)
        {
            if(a[i]==0&&coun==0)
            {
                b[i]=0;
            }
            else if(a[i]==2&&coun)
            {
                b[i]=0;
                coun=1;
            }
            else if(a[i]==1&&coun)
            {
                b[i]=1;
                coun=1;
            }
            else if(a[i]==0&&coun==1)
            {
                b[i]=0;
                coun=0;
            }
            else if(a[i]==2&&coun==0)
            {
                b[i]=1;
                coun=1;
            }
            else if(a[i]==1&&coun==0)
            {
                b[i]=0;
                coun=0;
            }




        }
//        for(int i=len-1;i>=0;i--)
//        {
//            printf("%d",a[i]);
//        }
//        puts("");
//
//        for(int i=len-1;i>=0;i--)
//        {
//            printf("%d",b[i]);
//        }
//        puts("");


        coun=0;
        int ll=0;
        for(int i=0;i<len;i++)
        {
            int tmp=0;
            tmp=a[i]+b[i]+coun;
            if(tmp==3)
            {
                coun=1;
                c[i]=0;
            }
            else if(tmp==0)
            {
                c[i]=0;
                coun=0;


            }
            else
            {
                c[i]=1;
                coun=0;
            }


        }
        if(coun)
        {
            ll=len+1;
            c[len]=1;
        }
        else
            ll=len;
        vector<int >ve;
        int bas=1;
        for(int i=0;i<len;i++)
        {
            if(b[i])
                ve.push_back(bas);
            bas*=3;
        }
        printf("%d",ve.size());


        for(int i=0;i<ve.size();i++)
        {
            printf(" %d",ve[i]);
        }
        puts("");
        ve.clear();
        bas=1;
        for(int i=0;i<ll;i++)
        {
            if(c[i])
                ve.push_back(bas);
                bas*=3;
        }
        printf("%d",ve.size());
        for(int i=0;i<ve.size();i++)
        {


            printf(" %d",ve[i]);
        }
        puts("");


    }


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值