Hard-题目49:273. Integer to English Words

题目原文:
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

For example,
123 -> “One Hundred Twenty Three”
12345 -> “Twelve Thousand Three Hundred Forty Five”
1234567 -> “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”
题目大意:
给出一个int类型的数字,输出英文读法。
题目分析:
难得一道原创代码。本题可以说毫无难度(我觉得应该放在easy里面,假期的时候是medium,不知道何时改成hard了)。大模拟而已,就是奇葩情况太多了(毕竟读数字乃是人脑思维),wa时对着test case一点点调试就好了。
源码:(language:java)

public class Solution {
    public String[][] words = {
            {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}, // 0 stand for 0-9
            {"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}, //1 stands for 11-19
            {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"} //2 stands for 10-90,please remember 'ninty' is WRONG.
    };
    public String numberToWords(int num) {
        if(num == 0)
            return "Zero";
        String result = "";
        if(num > 0 && num < 1000) {
            result=numberToWords_1_1000(num);
        }
        else if(num >= 1000 && num < 1000000) {
            result = numberToWords_1_1000(num/1000)+" Thousand";
            if(num%1000!=0)
                result = result + " "+numberToWords_1_1000(num%1000);
        }
        else if(num >= 1000000 && num < 1000000000) {
            result = numberToWords_1_1000(num/1000000)+" Million";
            if((num%1000000)/1000!=0) 
                result=result+" "+numberToWords_1_1000((num%1000000)/1000)+" Thousand";
            if(num%1000!=0)
                result=result+" "+numberToWords_1_1000(num%1000);

        }
        else {
            result = numberToWords_1_1000(num/1000000000)+" Billion";
            if((num%1000000000)/1000000!=0) 
                result=result+" "+numberToWords_1_1000((num%1000000000)/1000000) + " Million";
            if((num%1000000)/1000!=0) 
                result=result+" "+ numberToWords_1_1000((num%1000000)/1000)+" Thousand";
            if(num%1000!=0)
                result=result+" "+numberToWords(num%1000);      
        }
        return result;
    }
    private String numberToWords_1_100(int num) {
        if(num<10) // num=0 is a special case,return "" in order to recrusively call it. 
            return words[0][num];
        else if(num == 10)
            return words[2][0]; //return "Ten"
        else if(num < 20) // 11~19
            return words[1][num-11];
        else if(num % 10 == 0) // 20,30,...90
            return words[2][num/10-1];
        else
            return words[2][num/10-1]+" "+words[0][num%10];
    }
    private String numberToWords_1_1000(int num) {
        if(num<100)
            return numberToWords_1_100(num);
        else if(num==100)
            return "One Hundred";
        else if(num%100==0)
            return words[0][num/100]+" Hundred";
        else if(num>100 && num<200)
            return "One Hundred "+numberToWords_1_100(num%100);
        else
            return words[0][num/100]+" Hundred "+numberToWords_1_100(num%100);      
    }
}

成绩:
6ms,12.89%,5ms,44.43%
cmershen的碎碎念:
其实这道题有问题,小学英语老师就教过,百和十之间要有and,同理10^5和10^4,10^8和10^7之间也有,类似于1e6+5这样的数,规范读法是one million and five,在本题中and也省略了。另外测试用例中也没有负数,因此上述代码能够ac(但观察标程的输出可知,标程考虑了负数)
另外再次为大家补习一下小学甚至幼儿园的英语单词:19是nineteen,90是ninety!!!!!!!
ninteen和ninty根本不存在!也不知道谁教出来的这两个词,尤其是ninty。
不知道有多少高中生、大学生甚至研究生还犯这种低级错误……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值