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