概述
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.
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
2009 Multi-University Training Contest 6 - Host by WHU
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代码:
题目大意:
有一个天平,重量为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;
}
最后
以上就是包容羽毛为你收集整理的HDU 3029 Scales(三进制)Scales的全部内容,希望文章能够帮你解决HDU 3029 Scales(三进制)Scales所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复