题解 | #学英语#

学英语

http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

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","forteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    public static String[] twieties = new String[]{"zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
    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"};
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int num = sc.nextInt();
            System.out.println(transfer(num));
        }
    }
    public static String transfer(int num){
        if(num <= 9)return ones[num];
        if(num <= 19)return tens[num%10];
        if(num <= 99)return twieties[num/10] + (num%10 == 0 ? "" : " " + ones[num%10]);
        for(int i = 0; i<4; i++){
            if(num < range[i + 1]){
                return transfer(num/range[i]) + " " + ranges[i] + (num % range[i] == 0? " " :(i!=0?" " : " and ") + transfer(num % range[i]));
            }
        }
        return "";
    }
}
全部评论

相关推荐

我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务