题解 | #学英语#stack
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
import java.math.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.stream.*;
import java.util.regex.*;
import java.util.function.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
String[] levels = new String[] {"", "thousand", "million", "billion"};
LinkedList<String> stack = new LinkedList<>();
int levelsIndex = 0;
while (num % 1000 != 0) {
if (levelsIndex > 0) {
stack.push(levels[levelsIndex]);
}
stack.push(getEnglish(num % 1000));
levelsIndex++;
num = num / 1000;
}
// 技巧
System.out.println(String.join(" ", stack));
}
static String getEnglish(int num) {
List<String> list = new ArrayList<>();
String[] hundred = new String[] {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String[] ten = new String[] {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String[] tenTwenty = new String[] {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
if (num / 100 > 0) {
list.add(String.format("%s hundred", hundred[num / 100]));
}
num = num % 100;
if (num >= 10 && num < 20) {
list.add(tenTwenty[num - 10]);
} else if (num >= 20) {
if (num % 10 == 0) {
list.add(String.format("%s", ten[num / 10]));
} else {
list.add(String.format("%s %s", ten[num / 10], hundred[num % 10]));
}
} else if (num < 10 && num > 0) {
list.add(hundred[num % 10]);
}
return String.join(" and ", list);
}
}