wustoj1284Gold Medal(三进制思想)

1284: Gold Medal

Time Limit: 1 Sec   Memory Limit: 128 MB
[ Submit][ Status][ Web Board]

Problem Description

Gold medal is the aim of every one in WHUACM training team. Once upon a time, when Xioumu was a new member of team, he used to be a very curious boy. One day when he saw the gold medal of big cow Xay. Xioumu wanted to know the total weight of Xay‟s gold medal. Unfortunately,there are just exactly N weights and one balance. And the mark in i-th weights is 3^(i-1). So these N weights are 1, 3, 9, 27… Xioumu could put these weights on the left side of balance or right side of balance arbitrarily. Well, the gold medal is always on the left side. Now give you N and the weight of gold medal, please output the way to measure it. If there is more than one way, please output the way with the least number of weights. If Xioumu could not measure the gold medal with these weights, output “No way!”.

Input

There are several test cases. For each case there are two integer numbers. The number of weights N and the weight of the gold medal W . Technical Specification 1 <= N <= 30, 1 <= W <= 1,000,000,000

Output

For each case, if there exists a way to measure it. Then the output should contain two lines, the first line start with “LEFT:” and output the weights in the left side in ascending order. The second line start with “RIGHT:” and output the weights in the right side in ascending order. Else, just output “No way!”. Print a blank line after each case. See the following samples for more details.

Sample Input

4 14
2 14
2 3

Sample Output

LEFT:1 3 9
RIGHT:27

No way!

LEFT:
RIGHT:3

HINT

Source

Difficulty



题目大意:题面很清楚,给定n,w。开始天平左边放w重的物品。然后有1,3,9...,3^n-1这样的砝码可以使用。选取这样的砝码,通过左右摆放使得砝码能够平衡。

    解题思路:开始看题目说的意思有多组答案。。想到了爆搜。我们直接将开始的重量转换为三进制,然后通过给加法使得每位的权值为1或者为0,所要加的值便是左边需要放的砝码,加起来得到的数便是右边需要摆放的砝码。答案肯定唯一。


AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

int a[35];  //表示w的三进制表示。
int b[35];  //表示left砝码
int c[35];  //表示right砝码
int res1[35];
int res2[35];
int t;

int mypow(int base,int n)
{
    int ans=1;
    while(n--)
        ans=ans*base;
    return ans;
}

void tran(int w)
{
    t=0;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    while(w)
    {
        a[t++]=w%3;
        w/=3;
    }

    /*for(int i=0;i<t;i++)
        printf("%d ",a[i]);
    printf("\n");*/
}

void cal()
{
    int i;
    for(i=0;i<t+1;i++)
    {
        if(a[i]==0)
        {
            b[i]=0,c[i]=0;
        }
        else if(a[i]==1)
        {
            b[i]=0,c[i]=1;
        }
        else if(a[i]==2)
        {
            b[i]=1,c[i]=0;
            a[i+1]++;
        }
        else if(a[i]==3)
        {
            b[i]=0,c[i]=0;
            a[i+1]++;
        }
    }
}

int main()
{
    int n,w,i;

    while(cin>>n>>w)
    {
        tran(w);
        cal();

        int ans=0;
        for(i=t;i>=0;i--)
        {
            if(b[i]!=0||c[i]!=0)
            {
                ans=i+1;
                break;
            }
        }

        if(ans>n)
        {
            printf("No way!\n\n");
            continue;
        }

        int t1=0,t2=0;
        for(i=0;i<t+1;i++)
        {
            if(b[i])
            {
                res1[t1++]=mypow(3,i);
            }
            if(c[i])
            {
                res2[t2++]=mypow(3,i);
            }
        }

        printf("LEFT:");
        for(i=0;i<t1;i++)
        {
            if(i>0) printf(" ");
            printf("%d",res1[i]);
        }
        printf("\nRIGHT:");
        for(i=0;i<t2;i++)
        {
            if(i>0) printf(" ");
            printf("%d",res2[i]);
        }
        printf("\n\n");
    }
    return 0;
}

/*
4 14
2 14
2 3
*/



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值