Created
May 18, 2025 02:37
-
-
Save lambdaydoty/a9189b05e616f6d36ac594c14422f918 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
System.out.println("License Plates Generator"); | |
System.out.println(gen(3)); | |
System.out.println(gen(10000)); | |
System.out.println(gen(9000_00)); | |
} | |
private static String gen(int n) { | |
// [0,100_000) -> "" ... 100_000 | |
// [100_000, 110_000) -> "A" ... 10_000 | |
// [110_000, 120_000) -> "B" | |
// ... | |
// "Z" ... 10_000 | |
// "A" "A" ... 1_000 | |
// "A" "B" ... 1_000 | |
// ... | |
// "Z" "Z" ... 1_000 | |
// "A" "A" "A" ... 100 | |
int m = n-1; | |
if (m < 100_000*1) { | |
return String.format("%5d", m); | |
} | |
m -= 100_000*1; | |
if (m < 10_000*26) { | |
int k = m/10_000; | |
return String.format("%4d%c", m%10_000, (char)(m/10_000+'A')); | |
} | |
m -= 10_000*26; | |
if (m < 1_000*26*26) { | |
return String.format( | |
"%3d%c%c", | |
m%1_000, | |
(char)((m/1_000/26)+'A'), | |
(char)((m/1_000%16)+'A')); | |
} | |
m -= 1_000*26*26; | |
if (m < 100*26*26*26) { | |
return ""; | |
} | |
m -= 100*26*26*26; | |
if (m < 10*26*26*26*26) { | |
return ""; | |
} | |
m -= 10*26*26*26*26; | |
if (m < 1*26*26*26*26*26) { | |
return ""; | |
} | |
throw new IllegalStateException(); | |
} | |
/* | |
00000 | |
00001 | |
00002 <- n=3 | |
........ | |
........ | |
99999 | |
0000A | |
0001A | |
0002A | |
........ | |
......... | |
9999A | |
0000B | |
0001B | |
0002B | |
......... | |
......... | |
9999B | |
0000C | |
........ | |
........ | |
9999Z | |
000AA | |
001AA | |
......... | |
......... | |
999AA | |
000AB | |
.......... | |
.......... | |
999ZZ | |
00AAA | |
........ | |
........ | |
ZZZZZ | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment