hoj 1025 SkewBinary 题解

把题复制过来:

It had been a year since Swamp County Computing established a functional programming group. Your (non-functional programming) group is going to throw a surprise party for the anniversary. Now the functional folks really like skew binary numbers for some reason. "Easy to increment and decrement!" they say. Your task is to write a program to convert decimal integers to skew binary in the format they like. This will help in making banners and other party material.

Number representations are made up of a list of digits. Call the lowest order digit the rank 0 digit, the next, rank 1, etc. For example, in decimal representation, digits are 0-9, the rank 0 digit has weight 1, the rank 1 digit has weight 10, and the rank i digit has weight 10i. In binary representation, the digits are 0 and 1, and the rank i digit has weight 2i. In skew binary representation, the digits are 0, 1, and 2, and the rank i digit has weight 2^(i+1)-1.

						Rank	Weight	
						0	1	
						1	3	
						2	7	
						3	15		
						4	31	
						5	63	
						6	127	
						7	255	
						. :	. :	

Allowing the digit 2 in the skew binary means there may be several ways to represent a given number. However the convention is that the digit 2 may only appear as the lowest rank non-zero digit. This makes the representation unique.

In this problem, you should use a special way to write skew binary numbers as a list of ranks of non-zero digits in the number. The digit 2 is represented by the rank of the digit appearing twice in the list. Note that this means that only the first two ranks in the list may be equal.

Each rank is a decimal integer, and is separated from the next rank by a comma (','). A list is started by a '[' and ended by a ']'. For example, the decimal number 5, which has the skew representation 12, should be written as [0,0,1]. Decimal 0 is an empty list: [].

Input consists of decimal numbers, one per line
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by t lines, each containing a single decimal number with no leading or trailing white space. Each number will be in the range 0...100663270 (inclusive).

Output
There should be one line per test case containing the input decimal number, with no leading zeros or spaces, a single space, and the skew binary equivalent in list format with no leading or trailing spaces. Within the list each rank should have no extra leading zeros or leading or trailing spaces.

Sample Input

5
0
1
2
3
4
Sample Output
0 []
1 [0]
2 [0,0]
3 [1]
4 [0,1]

开始没有头绪啊,第一感觉就是想在网上找找,可惜网上的都不是一个题额。

后来看了下一个ppt,关于进制转换的,里面稍微提到了非等比转换,然后就一拍脑袋觉得有方法了。

关键代码:

while(temp != 0)
        {
            k = (int)(log((float)temp)/log(2.0)) ;
            if(temp >= (int)pow(2.0,k+1)-1)
                skewbinary[i].digit[(skewbinary[i].position)] = k ;
            else
                skewbinary[i].digit[(skewbinary[i].position)] = k-1 ;
            temp-=(int)(pow(2.0,skewbinary[i].digit[(skewbinary[i].position)]+1)-1) ;
            (skewbinary[i].position)++ ;  
        }
通过对2取对数,获得位数,不过有可能恰恰是高一位的,比如7,既可以是21,也可以是100,当然根据题意只能是前者,所以加上
 if(temp >= (int)pow(2.0,k+1)-1)
可以避免前者的出现。

幸好网上没有题解,不然我又要一抄了事了。

我也一直在怀疑,咱们编程,到底该使劲的想几天琢磨一个方法,还是想一下就去网上看代码呢。

我真的不知道。我们需要学习,我们也需要独立的思考。就像挖井,如果一个地方挖不出来,是选择换个地方挖,还是继续挖下去呢。不论哪种结果,挖出水的人都可以笑采取另一种方式的人。所以,没有必然,全靠运气。

这次,我被迫选了,继续挖。

附上代码:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    struct SkewBinary
    {
        int decimal ;
        int digit[31] ;
        int position ;
    } ;
    int n ;
    cin >> n ;
    struct SkewBinary skewbinary[n] ;
    for(int i = 0 ; i < n ; i++)
    {
        cin >>skewbinary[i].decimal ;
        (skewbinary[i].position) = 0 ;
        int temp = skewbinary[i].decimal ;
        if(temp == 0)
        continue ; 
        int k = 0 ;
        while(temp != 0)
        {
            k = (int)(log((float)temp)/log(2.0)) ;
            if(temp >= (int)pow(2.0,k+1)-1)
                skewbinary[i].digit[(skewbinary[i].position)] = k ;
            else
                skewbinary[i].digit[(skewbinary[i].position)] = k-1 ;
            temp-=(int)(pow(2.0,skewbinary[i].digit[(skewbinary[i].position)]+1)-1) ;
            (skewbinary[i].position)++ ;  
        }
    }
    for(int i = 0 ; i <n ; i++)
    {
         cout <<skewbinary[i].decimal <<" ["; 
         for(int j = (skewbinary[i].position) ;j >0 ; j--)
         {
              cout <<skewbinary[i].digit[j-1] ; 
              if(j!= 1)
              cout <<"," ; 
         }
         cout <<"]" <<endl; 
     
    }
    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值