Geek Challenge [SKRZAT] (Base Minus Two)
InterviewTime limit1sMemory limit128 MB
Convert numbers between decimal and base -2 (Weird Binary), echoing each query with its formatted result.
- Level
Easy3 of 10
- Topics
- Math, Implementation, Number theory
- Solved
- No attempts yet
Problem
Geek Challenge [SKRZAT] is a very old game from Poland, played on a console with two buttons and a joystick. True to its name, the game communicates in binary: one button means zero and the other means one. It goes one step further and uses a number system whose base is minus two instead of plus two — a representation we will call “Weird Binary”.
In Weird Binary the bit in position (counted from the right, starting at ) has weight . The value of a bit string is the sum of the weights of the positions that hold a one. The five-bit patterns and their values are:
Every five-bit pattern, ordered by bit pattern:
The same values, ordered by magnitude:
Numbers appear on the screen in Weird Binary, and the player answers from the console with a stream of zeroes and ones. Write a program that helps a novice by translating numbers between decimal and Weird Binary.
Input
The first line contains a single integer , the number of queries. Each of the next lines is one query:
- a line beginning with
bcontains a Weird Binary string that must be converted to decimal; - a line beginning with
dcontains a decimal integer that must be converted to Weird Binary.
Every value fits in a 15-bit Weird Binary number, so the decimal range is to inclusive.
Output
Print one line per query, echoing the query type and its input string together with the converted result:
- for a
bquery:From binary: <string> is <decimal> - for a
dquery:From decimal: <string> is <weird binary>
Reproduce the input string exactly (a Weird Binary input may contain leading zeroes). In a produced Weird Binary result leading zeroes are not allowed, and the value is written as 0. Match the spacing shown above exactly.