这个题需要注意的两个点
一个是 空格 (\doge 我特别想全加上空格 然后把两个及以上的空格替换成一个空格 最后再trim()一下 这样会少些很多判断)
一个是 and
import java.util.*;
public class Main {
static String[] N1_20 = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten","eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};
static String[] N10_TIMES = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
static String[] POWER = {"", " thousand ", " million ", " billion "};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] strs = String.format("%,d", in.nextInt()).split(",");
String s = "";
for (int i = strs.length - 1, j = 0; i >= 0; i--, j++) {
s = three(Integer.parseInt(strs[i])) + POWER[j] + s;
}
System.out.println(s);
}
static String three(int x) {
String s = "";
s = (x / 100) > 0 ? s + N1_20[x / 100] + " hundred" : s;
s += two(x % 100, (x / 100) > 0);
return s;
}
static String two(int a, boolean flag) {
if (a > 20) {
// 如果百位是0 就没有and 如果个位是0 就不加空格
return (flag ? " and " : "") + N10_TIMES[a / 10] + (a % 10 > 0 ? " " + N1_20[a % 10] : "");
} else if (a > 0) {
return (flag ? " and " : "") + N1_20[a];
} else {
return "";
}
}
}

京公网安备 11010502036488号