题解 | #学英语#
学英语
http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
import java.util.*;
public class Main {
private static LinkedHashMap<Long,String> map = new LinkedHashMap<>();
static{
map.put(0L,"");
map.put(1L,"one");
map.put(2L,"two");
map.put(3L,"three");
map.put(4L,"four");
map.put(5L,"five");
map.put(6L,"six");
map.put(7L,"seven");
map.put(8L,"eight");
map.put(9L,"nine");
map.put(10L,"ten");
map.put(11L,"eleven");
map.put(12L,"twelve");
map.put(13L,"thirteen");
map.put(14L,"fourteen");
map.put(15L,"fifteen");
map.put(16L,"sixteen");
map.put(17L,"seventeen");
map.put(18L,"eighteen");
map.put(19L,"nineteen");
map.put(20L,"twenty");
map.put(30L,"thirty");
map.put(40L,"forty");
map.put(50L,"fifty");
map.put(60L,"sixty");
map.put(70L,"seventy");
map.put(80L,"eighty");
map.put(90L,"ninety");
map.put(100L,"hundred");
map.put(1000L,"thousand");
map.put(1000000L,"million");
map.put(1000000000L,"billion");
}
private static long bl = 1000000000L;
private static long ml = 1000000L;
private static long td = 1000L;
private static long hd = 100L;
private static void printLongToEng(long num){
StringBuilder bd = new StringBuilder();
//num=1,111,652,510
while(num>0){
if(num/bl>0 && num/bl<10){//10位数:1,111,652,510
long n = num/bl;//n=1
num = num%10;//num=111,652,510
bd.append(map.get(n)).append(" ").append(map.get(bl)).append(" "); //几 billion
}else if(num/(bl/10)>0 && num/(bl/10)<10){//9位数:111,652,510
long n = num/(bl/10);//n=1
num = num%(bl/10);//num=11,652,510
if(n>0){
bd.append(map.get(n)).append(" ").append(map.get(hd)).append(" and "); //几百
}
}else if(num/ml>0 && num/ml<100){//8-7位数:11,652,510-1,652,510
long n = num/ml;//n=11
num = num%ml;//num=652,510
if(n>0){
String str = "";
if(n>=1 && n<=20){
str = map.get(n);
}else{
if(n%10!=0){
str = map.get((n/10)*10) +" " + map.get(n%10);
}else{
str = map.get((n/10)*10);
}
}
bd.append(str).append(" ").append(map.get(ml)).append(" "); //几十几million
}
}else if(num/(ml/10)>0 && num/(ml/10)<10){//6位数:652,510
long n = num/(ml/10);//n=6
num = num%(ml/10);//num=52,510
if(n>0){
bd.append(map.get(n)).append(" ").append(map.get(hd)).append(" and "); //几百
}
}else if(num/td>0 && num/td<100){//5-4位数:52,510-2,510
long n = num/td;//n=52
num = num%td;//num=510
if(n>0){
String str = "";
if(n>=1 && n<=20){
str = map.get(n);
}else{
if(n%10!=0){
str = map.get((n/10)*10) +" " + map.get(n%10);
}else{
str = map.get((n/10)*10);
}
}
bd.append(str).append(" ").append(map.get(td)).append(" "); //几十几thousand
}
}else if(num/hd>0 && num/hd<10){//3位数:510
long n = num/hd;//n=5
num = num%hd;//num=10
if(n>0){
bd.append(map.get(n)).append(" ").append(map.get(hd)).append(" and "); //几百
}
}else if(num<100){//2-1位数:10-0
String str = "";
if(num==0){
break;
}else if(num>=1 && num<=20){
str = map.get(num);
}else{
str = map.get((num/10)*10)+ " "+ map.get(num%10);//几十几
}
bd.append(str);
num = num/100;
}
}
String result = bd.toString();
if(result.endsWith(" and ")){
result = result.substring(0,result.lastIndexOf(" and "));
}
System.out.println(result);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
long num = sc.nextLong();
printLongToEng(num);
}
}
}
public class Main {
private static LinkedHashMap<Long,String> map = new LinkedHashMap<>();
static{
map.put(0L,"");
map.put(1L,"one");
map.put(2L,"two");
map.put(3L,"three");
map.put(4L,"four");
map.put(5L,"five");
map.put(6L,"six");
map.put(7L,"seven");
map.put(8L,"eight");
map.put(9L,"nine");
map.put(10L,"ten");
map.put(11L,"eleven");
map.put(12L,"twelve");
map.put(13L,"thirteen");
map.put(14L,"fourteen");
map.put(15L,"fifteen");
map.put(16L,"sixteen");
map.put(17L,"seventeen");
map.put(18L,"eighteen");
map.put(19L,"nineteen");
map.put(20L,"twenty");
map.put(30L,"thirty");
map.put(40L,"forty");
map.put(50L,"fifty");
map.put(60L,"sixty");
map.put(70L,"seventy");
map.put(80L,"eighty");
map.put(90L,"ninety");
map.put(100L,"hundred");
map.put(1000L,"thousand");
map.put(1000000L,"million");
map.put(1000000000L,"billion");
}
private static long bl = 1000000000L;
private static long ml = 1000000L;
private static long td = 1000L;
private static long hd = 100L;
private static void printLongToEng(long num){
StringBuilder bd = new StringBuilder();
//num=1,111,652,510
while(num>0){
if(num/bl>0 && num/bl<10){//10位数:1,111,652,510
long n = num/bl;//n=1
num = num%10;//num=111,652,510
bd.append(map.get(n)).append(" ").append(map.get(bl)).append(" "); //几 billion
}else if(num/(bl/10)>0 && num/(bl/10)<10){//9位数:111,652,510
long n = num/(bl/10);//n=1
num = num%(bl/10);//num=11,652,510
if(n>0){
bd.append(map.get(n)).append(" ").append(map.get(hd)).append(" and "); //几百
}
}else if(num/ml>0 && num/ml<100){//8-7位数:11,652,510-1,652,510
long n = num/ml;//n=11
num = num%ml;//num=652,510
if(n>0){
String str = "";
if(n>=1 && n<=20){
str = map.get(n);
}else{
if(n%10!=0){
str = map.get((n/10)*10) +" " + map.get(n%10);
}else{
str = map.get((n/10)*10);
}
}
bd.append(str).append(" ").append(map.get(ml)).append(" "); //几十几million
}
}else if(num/(ml/10)>0 && num/(ml/10)<10){//6位数:652,510
long n = num/(ml/10);//n=6
num = num%(ml/10);//num=52,510
if(n>0){
bd.append(map.get(n)).append(" ").append(map.get(hd)).append(" and "); //几百
}
}else if(num/td>0 && num/td<100){//5-4位数:52,510-2,510
long n = num/td;//n=52
num = num%td;//num=510
if(n>0){
String str = "";
if(n>=1 && n<=20){
str = map.get(n);
}else{
if(n%10!=0){
str = map.get((n/10)*10) +" " + map.get(n%10);
}else{
str = map.get((n/10)*10);
}
}
bd.append(str).append(" ").append(map.get(td)).append(" "); //几十几thousand
}
}else if(num/hd>0 && num/hd<10){//3位数:510
long n = num/hd;//n=5
num = num%hd;//num=10
if(n>0){
bd.append(map.get(n)).append(" ").append(map.get(hd)).append(" and "); //几百
}
}else if(num<100){//2-1位数:10-0
String str = "";
if(num==0){
break;
}else if(num>=1 && num<=20){
str = map.get(num);
}else{
str = map.get((num/10)*10)+ " "+ map.get(num%10);//几十几
}
bd.append(str);
num = num/100;
}
}
String result = bd.toString();
if(result.endsWith(" and ")){
result = result.substring(0,result.lastIndexOf(" and "));
}
System.out.println(result);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
long num = sc.nextLong();
printLongToEng(num);
}
}
}