数论进制水题

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.
 

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.
 

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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值