Problem 17 Number letter counts (统计字母)

本文介绍了一种计算从1到1000所有数字英文表达中字母总数的方法,并提供了完整的C++代码实现。文章通过分解数字范围(1-9, 10-19, 20-99, 100-999, 1000),分别计算各区间内数字的字母长度总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Number letter counts

Problem 17

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.


Answer:
21124
Completed on Thu, 27 Oct 2016, 05:16

题解:从1算到100,再从100 算到1000.

代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
  //1-9
  int s1[] = { 0, 3, 3, 5, 4, 4, 3, 5, 5, 4 };
 //10-19
  int s2[] = { 3, 6, 6, 8, 8, 7, 7, 9, 8, 8 };
  //20 30 40 50 60 70 80 90
  int s3[] = { 0, 0, 6, 6, 5, 5, 5, 7, 6, 6 };

  int hundred = 7;
  int And = 3;
  int thousand = 8;

  int i;
  int sum = 0;
  
  //1-9
  for (i = 1; i < 10; i++)
  {
    sum += s1[i];
  }
  
  //10-19
  for (i = 0; i < 10; i++){
    sum += s2[i];
  }
  
 //20-99
  for (i = 20; i < 100; i++){
    sum += s3[i/10] + s1[i%10];
  }
  
 //100-999
  for (i = 1; i < 10; i++)
  {
    int j;
   //计算hundred 
    sum += s1[i] + hundred;
    // i*100 + j
    for (j = 1; j < 10; j++){
      sum += s1[i] + hundred + And + s1[j];
    }
    //i*100 + j*10 
    for (j = 0; j < 10; j++) {
      sum += s1[i] + hundred + And + s2[j];
    }
    //i*100 +j*10 +k
    for (j = 20; j < 100; j++) {
      sum += s1[i] + hundred + And + s3[j/10] + s1[j%10];
    }
  }
  
  //1000
  sum += 3 + thousand;
  
  cout<<sum<<endl;
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值