Connection Status:
Competition Arena > ConvertBase
SRM 121 · 2002-11-26 · by mitalub
Class Name: ConvertBase
Return Type: String
Method Name: getValue
Arg Types: (int, int)
Problem Statement

Problem Statement

Given value, a positive or negative value in base 10, and toBase, a specified base, convert value to a number in base toBase. For example, if you are given the value 3 (in base 10) and the base 2, you must convert 3(base 10) to base 2, so you would return "11".

For the digits 10, 11, 12, 13, 14, and 15 (which may be used in bases 11 through 16), use the capital letters 'A', 'B', 'C', 'D', 'E', 'F', respectively.

For negative values, convert the absolute value of value to toBase, and then prepend a negative sign.

Notes

  • Your returned value should not have any leading 0's.
  • If the value is negative, the returned value should start with a "-" and then have the number in the new base.
  • There should be no spaces in the returned value.
  • If the returned value is 0, return "0" and not "-0".

Constraints

  • value will be between -10000 and 10000, inclusive.
  • base will be between 2 and 16, inclusive.
Examples
0)
2934
2
Returns: "101101110110"
1)
324
16
Returns: "144"
2)
-1231
4
Returns: "-103033"
3)
-15
16
Returns: "-F"
4)
234
12
Returns: "176"
7)
3
2
Returns: "11"

This is the example above 3(base 10) = 11(base 2)

8)
14
10
Returns: "14"

The toBase is 10, so the returned value is equal to the parameter value.

9)
18
16
Returns: "12"

18(base 10) = 12(base 16) because (1 * 16 ^ 1 + 2 * 16 ^ 0 = 18).

10)
0
13
Returns: "0"

Return "0", not "-0".

Submissions are judged against all 47 archived test cases, of which 9 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ConvertBase with a public method string getValue(int value, int toBase) · 47 test cases · 2 s / 256 MB per case

Submitting as anonymous