题解 | #学英语#
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); System.out.println(solution(num)); } private static final String[] base = new String[] { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty" }; private static final String[] tenP = new String[] { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; private static String solution(int num) { if (num <= 20) { return base[num]; } if (num < 100) { return tenP[num / 10 - 2] + ((num % 10 != 0) ? " " + solution(num % 10) : ""); } if (num < 1_000) { return base[num / 100] + " hundred" + (num % 100 != 0 ? " and " + solution( num % 100) : ""); } if(num < 1_000_000) { return solution(num / 1000) + " thousand" + (num % 1000 != 0 ? " " + solution(num % 1000) : ""); } if(num < 1_000_000_000) { return solution(num / 1_000_000) + " million" + (num % 1_000_000 != 0 ? " " + solution(num % 1_000_000) : ""); } return solution(num / 1_000_000_000) + " billion" + (num % 1_000_000_000 != 0 ? " " + solution(num % 1_000_000_000) : ""); } }