题解 | #学英语#
学英语
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); String[] nums = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; String[] ties = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; String[] units = {"", "thousand", "million", "billion"}; // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLong()) { // 注意 while 处理多个 case String ans = ""; int unit = 0; for (long n = in.nextLong(); n != 0; n /= 1000) { int m = (int) n % 1000, bit = m % 10, ten = m / 10 % 10, hundred = m / 100; String part = ""; if (hundred > 0) { part += nums[hundred] + " hundred "; if (ten > 0 || bit > 0) { part += "and "; } } if (ten < 2) { part += nums[m % 100] + " "; } else { part += ties[ten] + " "; if (bit > 0) { part += nums[bit] + " "; } } ans = part + units[unit++] + " " + ans; } System.out.println(ans); } } }