题解 | #学英语#
此题除了繁琐就是繁琐,完全就是消耗时间
import javax.swing.*; import java.util.*; public class Main { public static void main(String[] args) { // 创建字典表 Map<Integer, String> table = getTable(); // 处理输入 Scanner sc = new Scanner(System.in); long n = sc.nextLong(); String ns = n + ""; Deque<Character> stack = new LinkedList<>(); for (char ch : ns.toCharArray()) { stack.push(ch); } // 从右到左提取三位数字 int name = 0; // 记录每个结束的三位数字的单位,0-无,1-thousand, 2-billion List<String> english = new ArrayList<>(); // 存储每三位的英语读法 while (!stack.isEmpty()) { StringBuilder ansPart = new StringBuilder(); StringBuilder sb = new StringBuilder(); if (stack.size() >= 3) { sb.append(stack.pop()); sb.append(stack.pop()); sb.append(stack.pop()); sb.reverse(); } else if (stack.size() == 2) { sb.append(stack.pop()); sb.append(stack.pop()); sb.reverse(); } else { sb.append(stack.pop()); } // 获取到右边三位数字 String part = sb.toString(); int len = part.length(); if (len == 3) { boolean flag = false; // 判断第三位是否需要单独读取 // 第一位有数字,输出英文加上" hundred" if (table.containsKey(part.charAt(0) - '0')) { ansPart.append(table.get(part.charAt(0) - '0')).append(" hundred"); } // 第二位有数字 if (table.containsKey(part.charAt(1) - '0')) { // 第三位需要单独读取 if (part.charAt(1) - '0' > 1) { // 第一位有数字,需要加上"and " if (table.containsKey(part.charAt(0) - '0')) { ansPart.append(" and "); } ansPart.append(table.get((part.charAt(1) - '0') * 10)); flag = true; } else { // 第三位不需要单独读取 // 第一位有数字,需要加上"and " if (table.containsKey(part.charAt(0) - '0')) { ansPart.append(" and "); } int key = Integer.parseInt(part.substring(1, 3)); ansPart.append(table.get(key)); } } else { // 第二位没有数字,第三位需要单独读取 flag = true; } // 第三位有数字 if (table.containsKey(part.charAt(2) - '0') && flag) { // 第二位有数字 if (table.containsKey(part.charAt(1) - '0')) { ansPart.append(" "); } else if (table.containsKey(part.charAt(0) - '0')) { ansPart.append(" and "); // 第一位有数字,第二位没有数字,需要单独加" and " } ansPart.append(table.get(part.charAt(2) - '0')); } } else if (len == 2) { // 需要单独读取第二位 if (part.charAt(0) - '0' > 1) { // 第一位数字 if (table.containsKey(part.charAt(0) - '0')) { ansPart.append(table.get((part.charAt(0) - '0') * 10)); } // 第二位数字 if (table.containsKey(part.charAt(1) - '0')) { // 第一位数字存在,第二位数字需要加上" " if (table.containsKey(part.charAt(0) - '0')) { ansPart.append(" "); } ansPart.append(table.get(part.charAt(1) - '0')); } } else { // 不需要单独读取第二位 ansPart.append(table.get(Integer.parseInt(part))); } } else if (len == 1) { ansPart.append(table.get(part.charAt(0) - '0')); } if (name == 1) { ansPart.append(" thousand"); } if (name == 2) { ansPart.append(" million"); } name++; // System.out.println(ansPart.toString()); english.add(ansPart.toString()); } for (int i = english.size() - 1; i >= 0; i--) { System.out.print(english.get(i)); if (i != 0) { System.out.print(" "); } } } public static Map<Integer, String> getTable() { Map<Integer, String> table = new HashMap<>(); table.put(1, "one"); table.put(2, "two"); table.put(3, "three"); table.put(4, "four"); table.put(5, "five"); table.put(6, "six"); table.put(7, "seven"); table.put(8, "eight"); table.put(9, "nine"); table.put(10, "ten"); table.put(11, "eleven"); table.put(12, "twelve"); table.put(13, "thirteen"); table.put(14, "fourteen"); table.put(15, "fifteen"); table.put(16, "sixteen"); table.put(17, "seventeen"); table.put(18, "eighteen"); table.put(19, "nineteen"); table.put(20, "twenty"); table.put(30, "thirty"); table.put(40, "forty"); table.put(50, "fifty"); table.put(60, "sixty"); table.put(70, "seventy"); table.put(80, "eighty"); table.put(90, "ninety"); return table; } }