HDU 3029 Scales(三进制)

28 篇文章 0 订阅
21 篇文章 0 订阅

Scales

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 517    Accepted Submission(s): 233


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.
 

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
 

Source
 

Recommend
gaojie

题目大意:
    有一个天平,重量为1,3,9,27……(3的k次方)的石头各一个,有一个重量为m的物品放在天平左边,现在在天平左右放石头是天平平衡。

解题思路:
    石头重量是3^k,所以可以联想到3进制。
    现将m转化为3进制数,则每一位可能有三种情况:
1、为0:不用处理。
2、为1:对面的天平相应的位上应为1,使这一位的重量平衡。
3、为2:由于每种石头只有一个,所以就不能在对面天平放石头,应该在这边对应位置1,是其进到下一位。
    最后得到左右天平各得到一个数字。有各位有几个1就有几个石头,第k位为1,则重量为1^k的石头放在这边天平。

附AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;

int n,len;
long long ans,l,r;
const int f[]={1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441,1594323,4782969,14348907,43046721,129140163,387420489};//打一个3^k的表,f[k]表示3^k

long long my_pow(long long x)//系统自带的pow对于long long级别数字的运算有误差
{
    long long res=1;
    while(x--)
        res*=10;
    return res;
}

void get(int n)
{
    long long res=0;
    int pos=0;
    while(n>0)
    {
        int this_pos=n%3;
        res=res*10+this_pos;
        n/=3;
        if(this_pos==1)
            r+=my_pow(pos);//右边对应重量石头加一
        if(this_pos==2)
        {
            l+=my_pow(pos);//左边对应石头加一
            n++;//进位
        }
        pos++;//处理下一位
    }
}

int getnum(long long x)//统计各位有几个1,也就是石头的个数
{
    int res=0;
    while(x>0)
    {
        if(x%10)
            ++res;
        x/=10;
    }
    return res;
}

void put(long long x)//将各个石头输出
{
    int pos=0;
    while(x>0)
    {
        if(x%10)
            printf(" %d",f[pos]);
        ++pos;
        x/=10;
    }
}

int main()
{
    while(~scanf("%d",&n))
    {
        l=r=0;//用一个long long表示各位石头的放置情况
        get(n);
        printf("%d",getnum(l));
        put(l);
        putchar('\n');
        printf("%d",getnum(r));
        put(r);
        putchar('\n');
    }
    
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值