hnu 13073 Ternarian Weights

Ternarian Weights
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 16, Accepted users: 12
Problem 13073 : No special judgement
Problem description

Back in the Hellenic era, there was a small island in the Mediterranean Sea known as Ternaria. It was close to Sparta, but because of its mountainous terrain the Spartans found it difficult to conquer and it remained an independent state until the great earthquake of 729BC when it sank beneath the sea. It had a remarkable civilization and some modern historians think it is the basis for the mythological city of Atlantis. Ternaria is still known for its fundamental contributions to science and mathematics, many of which were adopted by the Greeks and later by the Romans. For example, Ternarians were the first group to use the standard weight of pounds, which we still use today. Ternarian mathematics used base 3 for all its calculations. (Historians speculate that this was out of respect for King Ternary who lost two fingers on each hand while battling the Spartans.)
Ternarian trade scales were a standard for many centuries. They were known for their accuracy and ease of use. They were the first to construct a scale with weighing pans on each side and a fulcrum in the middle. The object to be weighed was placed on the left side of the scale and weights were placed on both sides, until balance was obtained. This sounds strange by modern standards, because typically on modern scales we would only place weights on the right side. However, the modern method requires additional weights. The Ternarian method only requires one weight for each power of three pounds, e.g. one weight of 1 pound, one weight of 3 pounds, one weight of 9 pounds, etc. Say you are weighing a 2-pound Ternarian hen (known for their succulent white meat). You place the hen on the left side. Place the 3-pound weight on the right side. This is too heavy, so you place the 1-pound weight with the hen on the left side to achieve balance. Note that the sum of the weights on the right side minus the sum of the weights on the left side equals the weight of the object.<>/br As another example, consider weighing a 21-pound Ternarian squash. Using the Ternarian system, you would place weights of 27 pounds and 3 pounds in the right pan and a weight of 9 pounds in the left pan (along with the object) again achieving balance.
Write a program that accepts as input the weight of an object in base 10 and outputs the weights to be placed in both pans.


Input

The first line contains an integer 1<=n<=100, indicating how many test cases are to be solved. On each of the next n lines there is an integer 0<=x<=10^9 giving the weight of the object placed on the left scale.


Output

For each test case the program should produce two lines of output. The first line should contain left pan: followed by a space, followed by the weights to be placed in the left pan in descending order. The second line should contain right pan: followed by a space, followed by the weights to be placed in the right pan in descending order. Print a blank line between each pair of test cases.


Sample Input
4
2
3
21
250
Sample Output
left pan: 1
right pan: 3

left pan:
right pan: 3

left pan: 9
right pan: 27 3

left pan: 3
right pan: 243 9 1

题意就是给出一个东西的重量  在天平两边放砝码

砝码的重量只可能是3的n次方

要使砝码的个数最少  且各个重量的砝码只能放一个

感觉这个题蛮巧妙的  将十进制数转换成三进制数  然后去判断各个位上的数字

如果该位是2  则将把这个数转换为0  前面的一个数加一 

注意的是最高位为2的时候 也要加一 这个时候 三进制数的位数也要加一 

一开始  我就这样结束了  怎么都不对

然后 想到 如果该位为3  这个时候该位变为0  后一位加一 如果是最高位 同样位数要加一

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>

#define eps 1e-8
#define op operator
#define MOD  10009
#define MAXN  100100
#define INF 0x7fffffff

#define FOR(i,a,b)  for(int i=a;i<=b;i++)
#define FOV(i,a,b)  for(int i=a;i>=b;i--)
#define REP(i,a,b)  for(int i=a;i<b;i++)
#define REV(i,a,b)  for(int i=a-1;i>=b;i--)
#define MEM(a,x)    memset(a,x,sizeof a)
#define ll __int64

using namespace std;

int a[40];

ll ans(int x)
{
    ll tmp=1;
    for(int i=0;i<x;i++)
        tmp*=3;
    return tmp;
}

int main()
{
//freopen("ceshi.txt","r",stdin);
    int tc;
    scanf("%d",&tc);
    int flag=0;
    while(tc--)
    {
        int n;
        scanf("%d",&n);
        int num=0;
        MEM(a,0);
        while(n)
        {
            a[num++]=n%3;
            n/=3;
        }
//        for(int i=0;i<num;i++)
//            cout<<a[i];
//        cout<<endl;
        ll b[40];
        int cnt=0;
        int xxx=num;
        for(int i=0;i<num;i++)
        {
            if(a[i]==2)
            {
                b[cnt++]=ans(i);
                a[i]=0;
                a[i+1]++;
                if(i==num-1)
                {
                    xxx++;
                }
            }
            if(a[i]==3)
            {
                a[i]=0;
                a[i+1]++;
                if(i==num-1)
                {
                    xxx++;
                }
            }//第一次少判断了这种情况!!!
        }
//    for(int i=0;i<20;i++)
//        cout<<ans(i)<<endl;
        if(flag==1)
        {
//            flag=1;
            puts("");
        }
        else
            flag=1;
        printf("left pan:");
        for(int i=cnt-1;i>=0;i--)
        {
             printf(" %I64d",b[i]);
        }
        puts("");
//        cout<<"   "<<xxx<<endl;
//    for(int i=0;i<xxx;i++)
//        cout<<a[i];
//    cout<<endl;
        printf("right pan:");
        int f=1;

        for(int i=xxx-1;i>=0;i--)
        {
            if(a[i])
            {
                printf(" %I64d",ans(i));
            }
        }
        puts("");
    }
    return 0;
}



  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值