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.) |
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;
}