import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); //1≤n≤2000000 其实int类型就够了 while (in.hasNextLong()){ long num = in.nextLong(); invoke(num); } } private static void invoke(long num){ //定义千分位分隔模式对应的单位 String[] units = {null,"thousand","million","billion"}; //把输入的数字转成千分位分隔模式 String numstr = String.valueOf(num); int size = numstr.length() / 3; int offset = numstr.length() % 3; if(offset > 0){ size++; } List<String> res = new LinkedList<>(); for(int i=0;i<size;i++){ int begin =0,end=0; if(offset == 0){//输入数字正好是3的整数倍 begin = i*3; end = i*3 + 3; }else{ if(i > 0){ begin = offset + (i-1)*3; } end = offset + i*3; } int n = Integer.valueOf(numstr.substring(begin,end)); res.addAll(trans(n)); res.add(units[size-i-1]); } boolean first = true; for(String s : res){ if(s == null){//处理占位符,直接跳过当不存在即可 continue; } if(!first){ System.out.print(" "); } System.out.print(s); first = false; } } /** * 1-19 */ private static String[] numWords1_19 = {null,"one","two","three","four","five","six","seven","eight","nine", "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; /** * 20-90 */ private static String[] numWords20_90 = {"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; /** * 把三位以内的数字转成英文 * @param num * @return 为方便给单词加间隔,先暂存结果的所有单词再格式化打印 */ private static List<String> trans(int num){ List<String> res = new ArrayList<>(); int hundred = num/100; if(hundred > 0){ res.add(numWords1_19[hundred]); res.add("hundred"); } int other = num % 100; if(other == 0){ return res; } if(res.size() > 0){ res.add("and"); } if(other >= 20){ int ten = other / 10; res.add(numWords20_90[ten]); int one = other % 10; res.add(numWords1_19[one]); }else{ res.add(numWords1_19[other]); } return res; } }