project euler problem 17

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.


题意:就是求出1到1000的英文单词的字母之和。刚开始觉得有点麻烦,后面想了下就知道有规律了……

36  one two three four five six seven eight nine //因为后面20以的上都用到这些,所以放在一组
70  ten eleven twelve thirteen fourteen fifteen sixteen  seventeen eighteen nineteen  //这些是100以上的用到,所以也放一组
96  twenty...  // twenty总共有6个字母,故20到29的共有:6*10+36(第一组的)=96
96  thirty...  //同twenty:6*10+36(第一组的)=96
86  forty....  //同上计算:5*10+36(第一组的)=86
86  fifty....  //以下省略
86  sixty....
106  seventy.
96  eighty...
96  ninety...
总854字(1到99总共的字母数)
2151  one hundred and   //one hundred and总共有13个字母,故100到199的有:13*100+854(100以上包含0到99的,所以加上)-3(因为100没有and,故减3)=2151

2151  two hundred and      //下同……
2351  three hundred and
2251  four hundred and
2251  five hundred and
2151  six hundred and
2351  seven hundred and
2351  eight hundred and
2251  nine hundred and
11 One thousand 
以上各总的加起来就是答案:854+2251*9+11=21124

你可能感兴趣的:(project euler problem 17)