Created
February 25, 2020 22:41
-
-
Save Trumeet/2ffc5d3c0a3391443e28fee95105868d to your computer and use it in GitHub Desktop.
Cosmic Number
This file contains 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
import java.util.*; | |
/** | |
* Cosmic Number | |
* License: Unlicensed | |
**/ | |
public class Main { | |
private static final int BILLION = 1000000000; | |
private static final int MILLION = 1000000; | |
private static final int THOUSAND = 1000; | |
private static final int HUNDRED = 100; | |
private static final int TEN = 10; | |
private static final int ONE = 1; | |
public static void main(String[] args) { | |
final Scanner scanner = new Scanner(System.in); | |
int input = scanner.nextInt(); | |
scanner.nextLine(); | |
while (true) { | |
int last = 0; | |
for (final String term : numberToStr(input)) | |
last += term.length(); | |
if (input == last) break; | |
System.out.format("%d is %d, ", input, last); | |
input = last; | |
} | |
System.out.format("%d is cosmic\n", input); | |
} | |
private static List<String> numberToStr(final int input) { | |
int value = input; | |
List<String> terms = new ArrayList(5); | |
if (value >= BILLION) { | |
final int billions = value / BILLION; | |
value -= billions * BILLION; | |
terms.addAll(numberToStr(billions)); | |
terms.add("billion"); | |
} | |
if (value >= MILLION) { | |
final int millions = value / MILLION; | |
value -= millions * MILLION; | |
terms.addAll(numberToStr(millions)); | |
terms.add("million"); | |
} | |
if (value >= THOUSAND) { | |
final int thousands = value / THOUSAND; | |
value -= thousands * THOUSAND; | |
terms.addAll(numberToStr(thousands)); | |
terms.add("thousand"); | |
} | |
if (value >= HUNDRED) { | |
final int hundreds = value / HUNDRED; | |
value -= hundreds * HUNDRED; | |
terms.addAll(numberToStr(hundreds)); | |
terms.add("hundred"); | |
} | |
if (value >= TEN) { | |
final int tens = value / TEN; | |
value -= tens * TEN; | |
switch (tens) { | |
case 0: | |
break; | |
case 1: | |
// Extract right now. | |
final Pair<Integer, Integer> oneExtract = extractOnes(value); | |
value = oneExtract.first; | |
final int v = Integer.parseInt(new StringBuilder().append(tens /* 1 */).append(oneExtract.second).toString()); | |
switch (v) { | |
case 10: | |
terms.add("ten"); | |
break; | |
case 11: | |
terms.add("eleven"); | |
break; | |
case 12: | |
terms.add("twelve"); | |
break; | |
case 13: | |
terms.add("thirteen"); | |
break; | |
case 14: | |
terms.add("fourteen"); | |
break; | |
case 15: | |
terms.add("fifteen"); | |
break; | |
case 16: | |
terms.add("sixteen"); | |
break; | |
case 17: | |
terms.add("seventeen"); | |
break; | |
case 18: | |
terms.add("eighteen"); | |
break; | |
case 19: | |
terms.add("nineteen"); | |
break; | |
default: | |
throw new IllegalStateException("Unknown 10 ~ 19 value " + v); | |
} | |
break; | |
case 2: | |
terms.add("twenty"); | |
break; | |
case 3: | |
terms.add("thirty"); | |
break; | |
case 4: | |
terms.add("forty"); | |
break; | |
case 5: | |
terms.add("fifty"); | |
break; | |
case 6: | |
terms.add("sixty"); | |
break; | |
case 7: | |
terms.add("seventy"); | |
break; | |
case 8: | |
terms.add("eighty"); | |
break; | |
case 9: | |
terms.add("ninety"); | |
break; | |
default: | |
throw new IllegalStateException("Unknown tens " + tens); | |
} | |
} | |
final Pair<Integer, Integer> oneExtract = extractOnes(value); | |
value = oneExtract.first; | |
switch (oneExtract.second) { | |
case 0: | |
break; | |
case 1: | |
terms.add("one"); | |
break; | |
case 2: | |
terms.add("two"); | |
break; | |
case 3: | |
terms.add("three"); | |
break; | |
case 4: | |
terms.add("four"); | |
break; | |
case 5: | |
terms.add("five"); | |
break; | |
case 6: | |
terms.add("six"); | |
break; | |
case 7: | |
terms.add("seven"); | |
break; | |
case 8: | |
terms.add("eight"); | |
break; | |
case 9: | |
terms.add("nine"); | |
break; | |
default: | |
throw new IllegalStateException("Unknown digit " + oneExtract.second); | |
} | |
if (value != 0) throw new IllegalStateException("Unexpected left out"); | |
return terms; | |
} | |
private static Pair<Integer, Integer> extractOnes(int value) { | |
int ones = 0; | |
if (value >= ONE) { | |
ones = value / 1; | |
value -= ones * 1; | |
} | |
return new Pair(value, ones); | |
} | |
private static class Pair<A, B> { | |
public final A first; | |
public final B second; | |
public Pair(A first, B second) { | |
this.first = first; | |
this.second = second; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment