fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static final char[] DIGITS = ("0123456789"
  11. + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  12. + "abcdefghijklmnopqrstuvwxyz").toCharArray();
  13. private static final int BASE = DIGITS.length;
  14. private static final int MAX_NUMBER = BASE * BASE * BASE;
  15.  
  16. public static String generateCode(int num) {
  17. if (num < 1 || num > MAX_NUMBER) {
  18. throw new IllegalArgumentException("Illegal input value: " + num);
  19. }
  20.  
  21. int value = num - 1;
  22.  
  23. char ac = DIGITS[((value / BASE) / BASE) % BASE];
  24. char bc = DIGITS[(value / BASE) % BASE];
  25. char cc = DIGITS[value % BASE];
  26. return new String(new char[]{ac, bc, cc});
  27. }
  28.  
  29. public static void testIt(int value) {
  30. try {
  31. System.out.println("Value " + value + ": " + generateCode(value));
  32. } catch (Exception e) {
  33. System.out.println("Value " + value + " Error " + e.getMessage());
  34. }
  35. }
  36.  
  37. public static void main (String[] args) throws java.lang.Exception
  38. {
  39. testIt(0);
  40. testIt(1);
  41. testIt(2);
  42. testIt(62);
  43. testIt(63);
  44. testIt(1000);
  45. testIt(MAX_NUMBER);
  46. testIt(MAX_NUMBER + 1);
  47. }
  48. }
Success #stdin #stdout 0.1s 27740KB
stdin
Standard input is empty
stdout
Value 0 Error Illegal input value: 0
Value 1: 000
Value 2: 001
Value 62: 00z
Value 63: 010
Value 1000: 0G7
Value 238328: zzz
Value 238329 Error Illegal input value: 238329