编程介的小学生 2017-10-17 08:19 采纳率: 20.5%
浏览 1489
已采纳

Skew Binary

Description

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 2i+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

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]

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-11-01 15:47
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!