题解 | #学英语#

学英语

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 "";
    }
}
全部评论

相关推荐

手撕没做出来是不是一定挂
Chrispp3:不会,写出来也不一定过
点赞 评论 收藏
分享
斑驳不同:还为啥暴躁 假的不骂你骂谁啊
点赞 评论 收藏
分享
一名愚蠢的人类:多少games小鬼留下了羡慕的泪水
投递荣耀等公司10个岗位
点赞 评论 收藏
分享
11-27 17:08
已编辑
牛客_产品运营部_私域运营
腾讯 普通offer 24k~26k * 15,年包在36w~39w左右。
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务