表达数字的英文字母计数

如果把1到5写成英文单词,分别是:one, two, three, four, five,这些单词一共用了3 + 3 + 5 + 4 + 4 = 19个字母。

如果把1到1000都写成英文单词,一共要用多少个字母?

注意: 不要算上空格和连字符。例如,342(three hundred and forty-two)包含23个字母,而115(one hundred and fifteen)包含20个字母。单词“and”的使用方式遵循英式英语的规则。

#include<stdio.h>
int get_letter_nums(int x){
    static int arr1[20]={
        0,3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8
    };
    static int arr2[10]={0,0,6,6,5,5,5,7,6,6};
    if(x &lt; 20){
        return arr1[x];
    }else if (x&lt;100){
        return arr2[x / 10] + arr1[x % 10];
    }else if(x&lt;1000){
        if(x%100==0){
            return arr1[x / 100]+7;
        }
        return arr1[x / 100]+10+get_letter_nums(x % 100);
    }else{
        return 11;
    }
    
}
int main(){
    int ans=0;
    for(int i = 1; i &lt;= 1000; i++){
        ans+=get_letter_nums(i);
    }
    printf(\"%d\\n\",ans);
    return 0;
}

收藏 打印