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
题意:给出一个物品的重量,求在天平两端放重量为三的幂的石头。让天平平衡。
思路:首先将物品的重量转换为3进制,然后为2的位需要添加该位对应的幂的重量的石头。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define maxn 28
int a[maxn];
int ppow[maxn];
bool vis1[maxn];
vector <int> ans[2];
void init()
{
ppow[0] = 1;
for(int i = 1;i <= 20;i++)
ppow[i] = ppow[i-1]*3;
}
int main()
{
//freopen("in.txt","r",stdin);
init();
int m;
while(scanf("%d",&m)!=EOF)
{
ans[0].clear();
ans[1].clear();
memset(vis1,0,sizeof(vis1));
memset(a,0,sizeof(a));
int pos = 0;
while(m)
{
a[pos++] = m%3;
m/=3;
}
for(int i = 0;i < pos;i++)
{
if(a[i] == 3)
{
a[i] = 0;
a[i+1]++;
}
if(a[i] == 2)
{
vis1[i] = 1;
a[i] = 0;
a[i+1]++;
}
}
int ans1 = 0,ans2 = 0;
for(int i = 0;i <= pos;i++)
{
if(vis1[i] == 1)
{
ans[0].push_back(i);
ans1++;
}
if(a[i])
{
ans[1].push_back(i);
ans2++;
}
}
printf("%d",ans1);
for(int i = 0;i < ans[0].size();i++)
printf(" %d",ppow[ans[0][i]]);
printf("\n%d",ans2);
for(int i = 0;i < ans[1].size();i++)
printf(" %d",ppow[ans[1][i]]);
printf("\n");
}
return 0;
}