Connection Status:
Competition Arena > Vigesimal
SRM 142 · 2003-04-15 · by TangentZ
Class Name: Vigesimal
Return Type: String[]
Method Name: sorter
Arg Types: (vector<int>)
Problem Statement

Problem Statement

Most programmers are probably familiar with many different number systems such as binary (base 2), decimal (base 10), and hexadecimal (base 16). The Babylonians used a sexagesimal (base 60) system and we are still using this system to count time (60 seconds in 1 minute and 60 minutes in 1 hour). The ancient Mayan civilization had the unique vigesimal (base 20) number system which can still be seen in the French language. Presumably, the Mayans counted with all their fingers and toes.

For this problem, we will express the vigesimal numbers using "0123456789" and "abcdefghij" (quoted for clarity), like we do in the hexadecimal system. i.e. The digits have their usual values and a = 10, b = 11, c = 12, ..., j = 19.

For example:

  • 5 (base 20) = 5 (base 10) since 5*20^0 = 5
  • 16 (base 20) = 26 (base 10) since 1*20^1 + 6*20^0 = 26
  • 3g (base 20) = 76 (base 10) since 3*20^1 + 16*20^0 = 76
  • abc (base 20) = 4232 (base 10) since 10*20^2 + 11*20^1 + 12*20^0 = 4232

You are given a int[]. Your task is to write a function that expresses these integers in the vigesimal system as strings. The resulting strings should be sorted by their lengths in ascending order. If two strings have the same length, then the string with the lower rightmost digit that differs should come before the other. Your function will return the sorted strings as String[].

For example (quoted for clarity):

  • "g" comes before "h"
  • "24" comes before "6gf"
  • "13e" comes before "bcf"
  • "f3c2" comes before "ajhi2"
  • "bccc" comes before "adcc"

Constraints

  • numbers contains between 0 and 50 elements, inclusive
  • Each element of numbers is between 0 and 2,000,000,000, inclusive
Examples
0)
{20,60,40,41}
Returns: { "10",  "20",  "30",  "21" }

Note that "30" comes before "21" because 0 is less than 1.

1)
{14490,24372,18133,9747,3236,14338,5348}
Returns: { "d78",  "81g",  "1477",  "1g4a",  "30ic",  "256d",  "1fgi" }
2)
{}
Returns: { }

There is nothing to do, so return {}.

3)
{77, 78, 79, 87, 86, 85, 84, 83, 82, 81, 80}
Returns: { "40",  "41",  "42",  "43",  "44",  "45",  "46",  "47",  "3h",  "3i",  "3j" }
4)
{6574,99675,114645,27195,165143,18018,61101,62080,27620,155313,118480,176048,43753,145050,179315,16808,198806,77174,11733,1901,51349,157856,143604,59094,4208,89366,157296,124522,1019,88543,111812,154200,31410,184173,13223,114999,36624,26102,176474,38925,190257}
Returns: { "4f1",  "aa8",  "g8e",  "2aj",  "3910",  "7f40",  "eg40",  "j5a0",  "7cf1",  "3552",  "fb62",  "1d13",  "b173",  "hj04",  "4bb4",  "4h65",  "e6c5",  "b386",  "2208",  "6879",  "3iaa",  "i2ca",  "djac",  "j85d",  "196d",  "597d",  "77ee",  "9cie",  "c93f",  "37jf",  "jd4g",  "jecg",  "250i",  "e79j",  "10ch3",  "14h06",  "12028",  "1308d",  "1213e",  "1285f",  "13fch" }

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

Coding Area

Language: C++17 · define a public class Vigesimal with a public method vector<string> sorter(vector<int> numbers) · 65 test cases · 2 s / 256 MB per case

Submitting as anonymous