欧拉计划17题

Number letter counts

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到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”的使用方式遵循英式英语的规则。

  这个体通过把1-19分为一个区域,进行对1-19的英文字母的个数返回,把20-99化为一个区域,把100-999化为一个区域但是整百数也是一个特殊情况需要特殊处理,最后1000单独处理,这个题没有什么逻辑难度,主要是找规律以及对英文数组的理解,下面是代码实现:

#include <stdio.h>

int arr1[20] = {
    0, 3, 3, 5, 4 ,4, 3, 5, 5, 4, 3,
    6, 6, 8 ,8 ,7, 7, 9, 8, 8
};//1-19的英文字母个数

int arr2[10] = {
    0, 0, 6, 6, 5, 5, 5, 7, 6, 6
};//整10的英文字母个数


int get_lenght(int n) {
    if (n < 20) return arr1[n];
    else if (n < 100) return arr2[n / 10] + arr1[n % 10];
    else if (n < 1000) {
        if (n % 100 == 0) return arr1[n / 100] + 7;
        return arr1[n / 100] + 10 + get_lenght(n % 100);
    }
    return 11;
}


int main() {
    int ans = 0;
    for (int i = 1; i <= 1000; i++) ans += get_lenght(i);
    printf("%d\n", ans);
    return 0;
}

 最终答案:21124

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初猿°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值