题解 | #学英语#
学英语
http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
import java.util.*;
public class Main {
public static String[] power = {"thousand","million","billion"};
private static LinkedHashMap<Integer,String> map = new LinkedHashMap<>();
static{
map.put(1,"one");
map.put(2,"two");
map.put(3,"three");
map.put(4,"four");
map.put(5,"five");
map.put(6,"six");
map.put(7,"seven");
map.put(8,"eight");
map.put(9,"nine");
map.put(10,"ten");
map.put(11,"eleven");
map.put(12,"twelve");
map.put(13,"thirteen");
map.put(14,"fourteen");
map.put(15,"fifteen");
map.put(16,"sixteen");
map.put(17,"seventeen");
map.put(18,"eighteen");
map.put(19,"nineteen");
map.put(20,"twenty");
map.put(30,"thirty");
map.put(40,"forty");
map.put(50,"fifty");
map.put(60,"sixty");
map.put(70,"seventy");
map.put(80,"eighty");
map.put(90,"ninety");
map.put(100,"hundred");
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
long ln = in.nextLong();
String result = solveZheng(ln);
System.out.println(result);
}
}
public static String solveZheng(long zheng){
ArrayList<String> list = new ArrayList<>();
int pow = 0;
while(zheng != 0L){
//在最后一位加上单位,等到最后倒序输出
if(pow!=0){
list.add(" ");
list.add(power[pow-1]);
list.add(" ");
}
int temp = (int)(zheng % 1000);//除1000取余数,三位数
//个位
int gewei = temp % 10;
int shiwei = (temp / 10) % 10;
int baiwei = (temp/100) % 10;
int tempNum = shiwei*10 + gewei;
if(tempNum>0 && tempNum<20){
list.add(map.get(tempNum));
}else if(tempNum>20){
if(gewei>0){
list.add(map.get(gewei));
list.add(" ");
}
list.add(map.get(shiwei*10));
}
//百位
if(baiwei!=0){
if(gewei!=0 || shiwei !=0){
list.add(" and ");
}
list.add(" hundred");
list.add(map.get(baiwei));
}
zheng /= 1000; //源数据除以1000
pow++;//单位加一级
if(pow>3){ //单位超过3级,billion,则从thousand重新开始
pow=1;
}
}
//倒序输出
StringBuilder sb = new StringBuilder();
for(int i=list.size()-1;i>=0;i--){
sb.append(list.get(i));
}
return sb.toString().trim();
}
}
public class Main {
public static String[] power = {"thousand","million","billion"};
private static LinkedHashMap<Integer,String> map = new LinkedHashMap<>();
static{
map.put(1,"one");
map.put(2,"two");
map.put(3,"three");
map.put(4,"four");
map.put(5,"five");
map.put(6,"six");
map.put(7,"seven");
map.put(8,"eight");
map.put(9,"nine");
map.put(10,"ten");
map.put(11,"eleven");
map.put(12,"twelve");
map.put(13,"thirteen");
map.put(14,"fourteen");
map.put(15,"fifteen");
map.put(16,"sixteen");
map.put(17,"seventeen");
map.put(18,"eighteen");
map.put(19,"nineteen");
map.put(20,"twenty");
map.put(30,"thirty");
map.put(40,"forty");
map.put(50,"fifty");
map.put(60,"sixty");
map.put(70,"seventy");
map.put(80,"eighty");
map.put(90,"ninety");
map.put(100,"hundred");
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
long ln = in.nextLong();
String result = solveZheng(ln);
System.out.println(result);
}
}
public static String solveZheng(long zheng){
ArrayList<String> list = new ArrayList<>();
int pow = 0;
while(zheng != 0L){
//在最后一位加上单位,等到最后倒序输出
if(pow!=0){
list.add(" ");
list.add(power[pow-1]);
list.add(" ");
}
int temp = (int)(zheng % 1000);//除1000取余数,三位数
//个位
int gewei = temp % 10;
int shiwei = (temp / 10) % 10;
int baiwei = (temp/100) % 10;
int tempNum = shiwei*10 + gewei;
if(tempNum>0 && tempNum<20){
list.add(map.get(tempNum));
}else if(tempNum>20){
if(gewei>0){
list.add(map.get(gewei));
list.add(" ");
}
list.add(map.get(shiwei*10));
}
//百位
if(baiwei!=0){
if(gewei!=0 || shiwei !=0){
list.add(" and ");
}
list.add(" hundred");
list.add(map.get(baiwei));
}
zheng /= 1000; //源数据除以1000
pow++;//单位加一级
if(pow>3){ //单位超过3级,billion,则从thousand重新开始
pow=1;
}
}
//倒序输出
StringBuilder sb = new StringBuilder();
for(int i=list.size()-1;i>=0;i--){
sb.append(list.get(i));
}
return sb.toString().trim();
}
}