Basically Speaking

Interview

Time limit1sMemory limit128 MB

Summary
Convert a number from one base to another (bases 2 to 16) and print the result right-justified in a 7-character field, or ERROR if it exceeds 7 digits.
Level

Easy3 of 10

Topics
Math, Implementation, String, Simulation
Solved
No attempts yet

Problem

You are building the base-conversion feature of a pocket calculator. The calculator has the following properties:

  • It has a 7-digit display.
  • Its buttons include the digits 0-9 and the capital letters A-F.
  • It supports bases 2 through 16.

Write a program that converts a number from one base to another and prints how the result would appear on the calculator's display.

Input

Each line of input describes one conversion and contains three values separated by one or more spaces:

  1. the number to convert, written in the source base;
  2. the source base (an integer from 2 to 16);
  3. the target base (an integer from 2 to 16).

Extra spaces may appear before, between, or after the values. Keep reading and processing conversions until the end of the input.

Output

For each conversion, print the converted number exactly as it would appear on the 7-digit display: right-justified within a field of width 7 (padded with leading spaces). Digits greater than 9 are shown as the capital letters A-F.

If the converted number needs more than 7 digits it cannot fit on the display; in that case print ERROR instead, also right-justified within the width-7 field.

Examples1

  1. Example 1

    Input
    1111000  2 10
    1111000  2 16
    2102101  3 10
    2102101  3 15
      12312  4  2
         1A 15  2
    1234567 10 16
       ABCD 16 15
    
    Expected output
        120
         78
       1765
        7CA
      ERROR
      11001
     12D687
       D071