ConvertBase
SRM 121 · 2002-11-26 · by mitalub
Problem Statement
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.
2934 2 Returns: "101101110110"
324 16 Returns: "144"
-1231 4 Returns: "-103033"
-15 16 Returns: "-F"
234 12 Returns: "176"
3 2 Returns: "11"
This is the example above 3(base 10) = 11(base 2)
14 10 Returns: "14"
The toBase is 10, so the returned value is equal to the parameter value.
18 16 Returns: "12"
18(base 10) = 12(base 16) because (1 * 16 ^ 1 + 2 * 16 ^ 0 = 18).
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.
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