题解 | #学英语#
学英语
http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
import java.util.*;
public class Main { public static void main(String[] args) { String[] numbers = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"}; String[] numberWithZero = {"zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String[] powerIndex = {"", "hundred", "thousand", "million", "billion"};
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
long inputNumber = sc.nextLong();
StringBuilder result = new StringBuilder();
ArrayList<String> lists = new ArrayList<>();
int power = 1;
while(inputNumber != 0) {
if(power != 1) {
lists.add(powerIndex[power]);
}
int t =(int)(inputNumber % 1000);
if(t % 100 <= 20) {
if(t % 100 != 0) {
lists.add(numbers[t % 100]);
}
if(t / 100 != 0) {
if(t % 100 != 0) {
lists.add("and");
}
lists.add("hundred");
lists.add(numbers[t / 100]);
}
} else {
if(t % 10 != 0) {
lists.add(numbers[t % 10]);
}
t /= 10;
if(t % 10 != 0) {
lists.add(numberWithZero[t % 10]);
}
t /= 10;
if(t % 100 != 0) {
lists.add("and");
lists.add("hundred");
lists.add(numbers[t % 100]);
}
}
inputNumber /= 1000;
power++;
}
for(int i = lists.size() - 1; i >= 0; i--) {
if(i != 0) {
result.append(lists.get(i) + " ");
} else {
result.append(lists.get(i));
}
}
System.out.println(result.toString());
}
}
}