UVa 389

From Algorithmist
Jump to navigation Jump to search

389 - Basically Speaking[edit]

Summary[edit]

You have to create a conversion for a number in a given base to a number in another base. If the output exceeds 7 characters then you have to print error.

Explanation[edit]

The easiest way is to convert to base 10 and to convert to the second base. Here is the psuedo code:

n is the number.
do
    res=n%base   /*fix base after this ie base >=10*/
    n=n/base;
while( n != 0)

reverse(result) /*reverse the string*/

Gotchas[edit]

The 7 digit output should be right justified.

Input[edit]

1111000  2 10
1111000  2 16
2102101       3 10
        2102101  3 15
  12312  4  2
     1A 15  2
1234567 10 16
   ABCD 16 15
    000 12 12

Output[edit]

    120
     78
   1765
    7CA
  ERROR
  11001
 12D687
   D071
      0

References[edit]