import java.util.*; public class Main{ public static String[] ones = new String[]{"zero","one","two","three","four","five","six","seven","eight","nine"}; public static String[] tens = new String[]{"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; public static String[] wholeTen = new String[]{"zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; //wholeTen里带上0和10,是为了保证20在下标2的位置。但是用不到0和10 /*下面是科学计数法表示:e前面的是基数,e后面的是10的幂数,也是就是说: 1e2=1*(10的平方)=100,一百,one hundred; 1e3=1*(10的立方)=1,000,一千,one thousand; 1e6=1*(10的6次方)=1,000,000,一百万,one million; 1e9=1*10^9=1,0亿00,000,000,10亿,one billion; 1e12=1*10^12=1,000,0亿00,000,000,一万亿,one trillion。 题中说明: 数字为正整数,不考虑小数,转化结果为英文小写; 保证输入的数据合法 关键字提示:and,billion,million,thousand,hundred。 数据范围:1≤n≤2000000 ,2,000,000小于等于两百万。two million 小于等于2*1e6 1,652,510: one million six hundred and fifty two thousand five hundred and ten 1 million 6 5 2 thousand 5 10 */ public static int[] range = new int[]{(int)1e2, (int)1e3, (int)1e6, (int)1e9, (int)1e12}; public static String[] ranges = new String[]{"hundred","thousand","million","billion","trillion"}; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLong()){ long n = sc.nextLong();//输入一个long型整数 System.out.println(transformEnglish(n)); } } /* 1,652,510: one million six hundred and fifty two thousand five hundred and ten 1 million 6 5 2 thousand 5 10 */ private static String transformEnglish(long num) { if(num >= 0 && num <= 9) { return ones[(int) num];//0,1,2,3,4,5,6,7,8,9。 }else if(num >= 10 && num <= 19) { return tens[(int) num % 10];//10%10=0 ten,11%10=1 eleven,12%10=2 twelve,...,19%10=9。 }else if(num >= 20 && num <= 99) { return wholeTen[ (int) num / 10] + (num % 10 == 0 ? "" : (" " + ones[(int) (num % 10)]) ); //21= 20 + 21%10=20+1,20/10=2=wholeTen[2]=twenty,twenty one; // 99= 90 + 99%10 = 90+9,90/10=9=wholeTen[9]=ninety,ninety nine } /*注意:题目要求十位和个位之间不加-。下面先写上 * 100: one hundred 145: one hundred and forty-five = 145/100 = 145 / 1e2 = one hundred 1,234: one thousand two hundred and thirty-four 8,088: eight thousand (and) eighty-eight (注意:这个and可加可不加,这个题目我们选择不加) 486,669: four hundred and eighty-six thousand six hundred and sixty-nine 1,652,510: one million,six hundred and fifty-two thousand,five hundred and ten 1,235,365,953:one billion,two hundred and thirty-five million,three hundred sixty-five thousand,nine hundred fifty-three。 1,000,0亿00,000,000,一万亿,one trillion。 * * */ else if (num >= 100) { //递归调用 for (int i = 0; i < 4; i++) {//每循环一次,就会多一个单词hundred,thousand... if(num < range[ i + 1 ]){ //i=0,i+1=1,小于1e3,使用hundred;i=1,i+1=2,小于1e6,使用thousand; // i=2,i+1=3,小于1e9,使用million;i=3,i+1=4,小于1e12,使用billion。 /* return transformEnglish(num / range[i] )+ " " + ranges[i] + ( num % range[i] == 0 ? "" : (i != 0 ? " " : " and ") + transformEnglish(num % range[i]) );*/ String str = num % range[i] == 0 ? "" : (i != 0 ? " " : " and ") + transformEnglish(num % range[i]); /* i>0的时候,后面还有数字需要循环调用。 1,234=one thousand(i!=0,用空格)two hundred and thirty-four 100,023=one hundred thousand and twenty-three 但是此题要求这个and不用加。所以是one hundred thousand twenty-three 1=0时,145: one hundred and forty-five */ return transformEnglish(num / range[i] ) + " " + ranges[i] + str; } } } return ""; } }