您的位置:首页 > 其它

欧拉项目 第17题 Number letter counts

2016-03-17 14:45 211 查看
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.1-1000换成one,two。。。,one thousand 这种新式,共有多少字符?
static String hund = "hundred";
static String and = "and";
static String[] one = {"one","two","three","four","five","six","seven","eight","nine"};
static String[] ten = {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
static String[] t_2_0 = {"one","two","three","four","five","six","seven","eight","nine", "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    static Integer hund1 = 7;
    static Integer and1 = 3;
    static Integer[] one1 = {3,3,5,4,4,3,5,5,4};//36
    static Integer[] ten1 = {3,6,6,5,5,5,7,6,6};//49,去掉ten 46
    static Integer[] t_2_01 = {3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8}; //106
long length = 11;
    for(int i=1;i<1000;i++) {
            length += tt1(i);
    }
public static Integer tt1(int i) {
        if(i < 100) {
            if(i < 20) {
                return t_2_01[i - 1];
            } else if(i%10==0) {
                return ten1[i/10 - 1];
            } else {
                return ten1[i/10 - 1] + one1[i%10 - 1];
            }
        } else {
            if(i%100 == 0) {
                return one1[i/100 - 1] + hund1;
            } else if(i%100 < 20) {
                return one1[i/100 - 1] + hund1 + and1 + t_2_01[i%100 - 1];
            } else if(i%10==0) {
                return one1[i/100 - 1] + hund1 + and1 + ten1[(i%100)/10 - 1];
            } else {
                return one1[i/100 - 1] + hund1 + and1 + ten1[(i%100)/10 - 1] + one1[(i%100)%10 - 1];
            }
        }
    }
public static String tt(int i) { //得出数字字符        if(i < 100) {            if(i < 20) {                return t_2_0[i - 1];            } else if(i%10==0) {                return ten[i/10 - 1];            } else {                return ten[i/10 - 1] + one[i%10 - 1];            }        } else {            if(i%100 == 0) {                return one[i/100 - 1] + hund;            } else if(i%100 < 20) {                return one[i/100 - 1] + hund + and + t_2_0[i%100 - 1];            } else if(i%10==0) {                return one[i/100 - 1] + hund + and + ten[(i%100)/10 - 1];            } else {                return one[i/100 - 1] + hund + and + ten[(i%100)/10 - 1] + one[(i%100)%10 - 1];            }        }    }
可以循环上面方法运算可以归纳下,1-19 有 10个 , 20-90 有 100个, 1-9 有 80个(1-9算在1-19里面),100-900有100个,and有99 * 9个,1000一个结果就是106 * 10 + 46 * 100 + 36 * 80 + 99 * 100 + 3 * 99 * 9 + 11
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: