题解 | #学英语#
学英语
http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
import java.util.*;
public class Main{
private static Map<Integer, String> less20map = new HashMap<>();
private static Map<Integer, String> map = new HashMap<>();
static {
less20map.put(1, "one");
less20map.put(2, "two");
less20map.put(3, "three");
less20map.put(4, "four");
less20map.put(5, "five");
less20map.put(6, "six");
less20map.put(7, "seven");
less20map.put(8, "eight");
less20map.put(9, "nine");
less20map.put(10, "ten");
less20map.put(11, "eleven");
less20map.put(12, "twelve");
less20map.put(13, "thirteen");
less20map.put(14, "forteen");
less20map.put(15, "fifteen");
less20map.put(16, "sixteen");
less20map.put(17, "seventeen");
less20map.put(18, "eighteen");
less20map.put(19, "nineteen");
less20map.put(20, "twenty");
map.put(2, "twenty");
map.put(3, "thirty");
map.put(4, "forty");
map.put(5, "fifty");
map.put(6, "sixty");
map.put(7, "seventy");
map.put(8, "eighty");
map.put(9, "ninety");
}
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
StringBuilder sb = new StringBuilder();
if (str.length() > 9) {
//十亿以上
int b3 = Integer.parseInt(str.substring(0, str.length() - 9));
sb.append(translate(b3) + "billion");
//百万以上的三位数
int m3 = Integer.parseInt(str.substring(str.length() - 9, str.length() - 6));
if (m3 > 0) {
sb.append(" " + translate(m3) +" million");
}
//千以上的三位数
int t3 = Integer.parseInt(str.substring(str.length() - 6, str.length() - 3));
if (t3 > 0) {
sb.append(" " + translate(t3) + " thousand");
}
//末尾位数
int l3 = Integer.parseInt(str.substring(str.length() - 3));
if (l3 > 0) {
sb.append(" " + translate(l3));
}
} else if (str.length() > 6) {
//百万以上的三位数
int m3 = Integer.parseInt(str.substring(0, str.length() - 6));
if (m3 > 0) {
sb.append(translate(m3) +" million");
}
//千以上的三位数
int t3 = Integer.parseInt(str.substring(str.length() - 6, str.length() - 3));
if (t3 > 0) {
sb.append(" " + translate(t3) + " thousand");
}
//末尾位数
int l3 = Integer.parseInt(str.substring(str.length() - 3));
if (l3 > 0) {
sb.append(" " + translate(l3));
}
} else if (str.length() > 3) {
//千以上的三位数
int t3 = Integer.parseInt(str.substring(0, str.length() - 3));
if (t3 > 0) {
sb.append(translate(t3) + " thousand");
}
//末尾三位数
int l3 = Integer.parseInt(str.substring(str.length() - 3));
if (l3 > 0) {
sb.append(" " + translate(l3));
}
} else {
//末尾位数
int l3 = Integer.parseInt(str);
if (l3 > 0) {
sb.append(translate(l3));
}
}
System.out.println(sb.toString());
}
}
public static String translate(int num){
StringBuilder sb = new StringBuilder();
if (num >= 100) {
int i3 = num / 100;
sb.append(less20map.get(i3) +" hundred");
int i2 = num % 100;
if (i2 > 20) {
int i1 = i2 / 10;
sb.append(" and " + map.get(i1));
int i0 = i2 % 10;
if (i0 > 0) {
sb.append(" " + less20map.get(i0));
}
} else {
if (i2 > 0) {
sb.append(" and " + less20map.get(i2));
}
}
} else if (num > 20) {
int i1 = num / 10;
sb.append(map.get(i1));
int i0 = num % 10;
if (i0 > 0) {
sb.append(" " + less20map.get(i0));
}
} else {
if (num > 0) {
sb.append(less20map.get(num));
}
}
return sb.toString();
}
}
public class Main{
private static Map<Integer, String> less20map = new HashMap<>();
private static Map<Integer, String> map = new HashMap<>();
static {
less20map.put(1, "one");
less20map.put(2, "two");
less20map.put(3, "three");
less20map.put(4, "four");
less20map.put(5, "five");
less20map.put(6, "six");
less20map.put(7, "seven");
less20map.put(8, "eight");
less20map.put(9, "nine");
less20map.put(10, "ten");
less20map.put(11, "eleven");
less20map.put(12, "twelve");
less20map.put(13, "thirteen");
less20map.put(14, "forteen");
less20map.put(15, "fifteen");
less20map.put(16, "sixteen");
less20map.put(17, "seventeen");
less20map.put(18, "eighteen");
less20map.put(19, "nineteen");
less20map.put(20, "twenty");
map.put(2, "twenty");
map.put(3, "thirty");
map.put(4, "forty");
map.put(5, "fifty");
map.put(6, "sixty");
map.put(7, "seventy");
map.put(8, "eighty");
map.put(9, "ninety");
}
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
StringBuilder sb = new StringBuilder();
if (str.length() > 9) {
//十亿以上
int b3 = Integer.parseInt(str.substring(0, str.length() - 9));
sb.append(translate(b3) + "billion");
//百万以上的三位数
int m3 = Integer.parseInt(str.substring(str.length() - 9, str.length() - 6));
if (m3 > 0) {
sb.append(" " + translate(m3) +" million");
}
//千以上的三位数
int t3 = Integer.parseInt(str.substring(str.length() - 6, str.length() - 3));
if (t3 > 0) {
sb.append(" " + translate(t3) + " thousand");
}
//末尾位数
int l3 = Integer.parseInt(str.substring(str.length() - 3));
if (l3 > 0) {
sb.append(" " + translate(l3));
}
} else if (str.length() > 6) {
//百万以上的三位数
int m3 = Integer.parseInt(str.substring(0, str.length() - 6));
if (m3 > 0) {
sb.append(translate(m3) +" million");
}
//千以上的三位数
int t3 = Integer.parseInt(str.substring(str.length() - 6, str.length() - 3));
if (t3 > 0) {
sb.append(" " + translate(t3) + " thousand");
}
//末尾位数
int l3 = Integer.parseInt(str.substring(str.length() - 3));
if (l3 > 0) {
sb.append(" " + translate(l3));
}
} else if (str.length() > 3) {
//千以上的三位数
int t3 = Integer.parseInt(str.substring(0, str.length() - 3));
if (t3 > 0) {
sb.append(translate(t3) + " thousand");
}
//末尾三位数
int l3 = Integer.parseInt(str.substring(str.length() - 3));
if (l3 > 0) {
sb.append(" " + translate(l3));
}
} else {
//末尾位数
int l3 = Integer.parseInt(str);
if (l3 > 0) {
sb.append(translate(l3));
}
}
System.out.println(sb.toString());
}
}
public static String translate(int num){
StringBuilder sb = new StringBuilder();
if (num >= 100) {
int i3 = num / 100;
sb.append(less20map.get(i3) +" hundred");
int i2 = num % 100;
if (i2 > 20) {
int i1 = i2 / 10;
sb.append(" and " + map.get(i1));
int i0 = i2 % 10;
if (i0 > 0) {
sb.append(" " + less20map.get(i0));
}
} else {
if (i2 > 0) {
sb.append(" and " + less20map.get(i2));
}
}
} else if (num > 20) {
int i1 = num / 10;
sb.append(map.get(i1));
int i0 = num % 10;
if (i0 > 0) {
sb.append(" " + less20map.get(i0));
}
} else {
if (num > 0) {
sb.append(less20map.get(num));
}
}
return sb.toString();
}
}