鱼C论坛

 找回密码
 立即注册
查看: 4358|回复: 15

题目17:用英文写出1到1000的所有数字需要多少个字母?

[复制链接]
发表于 2015-4-21 16:45:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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(包含 1000)用英文写出,那么一共需要多少个字母?

注意: 空格和连字符不算在内。例如,342 (three hundred and forty-two)包含 23 个字母; 115 (one hundred and fifteen)包含 20 个字母。"and" 的使用与英国标准一致。  

评分

参与人数 1贡献 +1 收起 理由
cwhsmile + 1 只认识one到ten,多了不会。。。

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-25 23:22:00 | 显示全部楼层
  1. know={
  2. '0':0,
  3. '1':3,
  4. '2':3,
  5. '3':5,
  6. '4':4,
  7. '5':4,
  8. '6':3,
  9. '7':5,
  10. '8':5,
  11. '9':4,
  12. '10':3,
  13. '11':6,
  14. '12':6,
  15. '13':8,
  16. '14':8,
  17. '15':7,
  18. '16':7,
  19. '17':9,
  20. '18':8,
  21. '19':8,
  22. '20':6,
  23. '30':6,
  24. '40':6,
  25. '50':5,
  26. '60':5,
  27. '70':7,
  28. '80':6,
  29. '90':6,}
  30. total = 0
  31. for i in range(1,1001):
  32.       n = str(i)
  33.       if n in know:
  34.             total += know[n]
  35.       elif len(n) == 2 and n[0] != '1' and n[1]!='0':
  36.             total += know[n[0]+'0'] + know[n[1]]
  37.       elif len(n) == 3:
  38.             if n[1] == '0' and n[2] == '0':
  39.                   total += know[n[0]] + 7
  40.             elif n[1] != '0' and n[2] == '0':
  41.                   total += know[n[0]]+10 +know[n[1:]]
  42.             elif n[1] == '0' and n[2] != '0':
  43.                   total += know[n[0]] + 10 + know[n[2]]
  44.             else:
  45.                   if n[1:] in know:
  46.                         total += know[n[0]] + 10 + know[n[1:]]
  47.                   else:
  48.                         total += know[n[0]]+10+know[n[1]+'0'] + know[n[2]]
  49.       else:
  50.             total += 11  #加上one thousand
  51. print(total)
复制代码

不知道对不对 算出来是21224
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-27 14:02:56 | 显示全部楼层
  1. a = 'onetwothreefourfivesixseveneightnine'
  2. a2 = 'teneleventwelvethirteenfourteenfifteensixteenseventeeneighteennineteen'
  3. b = ['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninty']
  4. c = ['','onehundredand','twohundredand','threehundredand','fourhundredand',\
  5.      'fivehundredand','sixhundredand','sevenhundredand','eighthundredand',\
  6.      'ninehundredand']

  7. n = ''
  8. count = 0
  9. for i in c:
  10.     for j in range(10):
  11.         if j == 0:
  12.             n = n+i*10+a
  13.         if j == 1:
  14.             n = n+i*10+a2

  15.         if j != 0 and j != 1:
  16.             n = n+i*10+b[j]*10+a

  17. n = n+'onethousand'
  18. print(len(n))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-11 14:35:40 | 显示全部楼层
愤怒的大头菇 发表于 2016-8-25 23:22
不知道对不对 算出来是21224

你的算法是对的,但是Forty是5个字母,不是Fourty
所以你的答案多了100
正确的应该是21124
  1. know={
  2. '0':0,
  3. '1':3,
  4. '2':3,
  5. '3':5,
  6. '4':4,
  7. '5':4,
  8. '6':3,
  9. '7':5,
  10. '8':5,
  11. '9':4,
  12. '10':3,
  13. '11':6,
  14. '12':6,
  15. '13':8,
  16. '14':8,
  17. '15':7,
  18. '16':7,
  19. '17':9,
  20. '18':8,
  21. '19':8,
  22. '20':6,
  23. '30':6,
  24. '40':5,
  25. '50':5,
  26. '60':5,
  27. '70':7,
  28. '80':6,
  29. '90':6,
  30. '100':10,
  31. '200':10,
  32. '300':12,
  33. '400':11,
  34. '500':11,
  35. '600':10,
  36. '700':12,
  37. '800':12,
  38. '900':11}
  39. total = 0
  40. for i in range(1,1001):
  41.         n = str(i)
  42.         if n in know:
  43.                 total += know[n]
  44.         elif i>100 and i<1000:
  45.                 total += know[n[0]]+10
  46.                 if str(int(n[1:3])) in know:
  47.                         total += know[str(int(n[1:3]))]
  48.                 else:
  49.                         total += know[n[1]+'0']+know[n[2]]
  50.         elif i>20 and i<100 and n not in know:
  51.                 total += know[n[0]+'0']+know[n[1]]
  52.         else:
  53.                 total += 11
  54. print(total)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-16 15:06:22 | 显示全部楼层
  1. first_ten = ['one','two','three','four','five','six','seven','eight','nine','ten']
  2. second_ten = ['eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
  3. over_twenty = ['twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']

  4. def letter_trans(number):
  5.     result = []
  6.     if number == 1000:
  7.         return 'one thousand'
  8.     if number >= 100:
  9.         result.append(first_ten[int(str(number)[0])-1])
  10.         result.append('hundred')
  11.         number = int(str(number)[1:])
  12.         if number:
  13.             result.append('and')
  14.         else:
  15.             return ' '.join(result)
  16.     if number >= 20:
  17.         result.append(over_twenty[int(str(number)[0])-2])
  18.         number = int(str(number)[1:])
  19.         if number:
  20.             result.append(first_ten[number-1])
  21.         return ' '.join(result)
  22.     if 11 <= number <= 19:
  23.         result.append(second_ten[number-11])
  24.         return ' '.join(result)
  25.     if number <= 10:
  26.         result.append(first_ten[number-1])
  27.         return ' '.join(result)

  28. def euler(x):
  29.     list_x = []
  30.     for i in range(1,x+1):
  31.         list_x.append(len([l for l in letter_trans(i) if l.isalpha()]))
  32.     return sum(list_x)

  33. if __name__ == '__main__':
  34.     print(euler(1000))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-16 11:38:03 | 显示全部楼层
jerryxjr1220 发表于 2016-10-11 14:35
你的算法是对的,但是Forty是5个字母,不是Fourty
所以你的答案多了100
正确的应该是21124

谢谢,终于知道是哪里错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-10 15:44:34 | 显示全部楼层
  1. # encoding:utf-8
  2. # 英文写的1-1000数字,共有多少字母? 不计算空格和连接符
  3. from time import time
  4. dic = {1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',
  5.        10:'ten',11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'fifteen',16:'sixteen',
  6.        17:'seventeen',18:'eighteen',19:'nineteen',20:'twenty',30:'thirty',40:'forty',50:'fifty',
  7.        60:'sixty',70:'seventy',80:'eighty',90:'ninety',100:'hundred',1000:'thousand'}

  8. def euler017(N):
  9.     count = 0
  10.     for i in range(1,N+1):
  11.         l_result=[]
  12.         getChars(i,l_result)
  13.         #print(l_result)
  14.         count += sum(len(ch) for ch in l_result)
  15.     print(count)
  16.             
  17. def getChars(n,l_result):
  18.     if n == 100 or n == 1000:
  19.         l_result.append('one')
  20.         l_result.append(dic.get(n))
  21.     elif dic.get(n):
  22.         l_result.append(dic.get(n))
  23.     else:   
  24.         if n>100:
  25.             lenth = len(str(n))
  26.             tmp = int((n - n%(10**(lenth-1)))/10**(lenth-1))
  27.             l_result.append(dic.get(tmp))
  28.             l_result.append(dic.get(10**(lenth-1)))
  29.             n = n%10**(lenth-1)
  30.             if n:
  31.                 l_result.append('and')
  32.                 getChars(n,l_result)
  33.         else:
  34.             tmp = n - n%10
  35.             l_result.append(dic.get(int(n - n%10)))
  36.             l_result.append(dic.get(int(n%10)))
  37.             
  38. if __name__ == '__main__':
  39.     start = time()
  40.     euler017(1000)
  41.     print('cost %.6f sec' % (time() - start))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-18 11:20:21 | 显示全部楼层

大哥你的ninety,少了个e,害死我了,我去
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-18 11:27:41 | 显示全部楼层
此代码使用matlab编程
Problem17所用时间为0.014651秒
Problem17的答案为21124
  1. %题目17:用英文写出1到1000的所有数字需要多少个字母?
  2. function Output=Problem17(Input)
  3. tic
  4. if nargin==0
  5.     Input=1000;
  6. end
  7. One2nintynine=0;%1到99的值
  8. Sum=0;
  9. a = {'one','two','three','four','five','six','seven','eight','nine','ten'};
  10. a2 = {'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'};
  11. b = {'twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'};
  12. c = {'onehundredand','twohundredand','threehundredand','fourhundredand',...
  13.      'fivehundredand','sixhundredand','sevenhundredand','eighthundredand',...
  14.      'ninehundredand'};
  15. F1=zeros(1,length(a));
  16. F2=zeros(1,length(a2));
  17. S=zeros(1,length(b));
  18. T=zeros(1,length(c));
  19. for ii=1:length(a)
  20.     F1(ii)=length(a{ii});
  21. end
  22. for jj=1:length(a2)
  23.     F2(jj)=length(a2{jj});
  24. end
  25. for kk=1:length(b)
  26.     S(kk)=length(b{kk});
  27. end
  28. for ll=1:length(c)
  29.     T(ll)=length(c{ll});
  30. end
  31. for ii=1:Input/10-1  %99
  32.     if 1<=ii&&ii<=10
  33.         One2nintynine=One2nintynine+F1(ii);
  34.     elseif 11<=ii&&ii<=19
  35.         One2nintynine=One2nintynine+F2(ii-10);
  36.     else
  37.         Num=str2num(num2str(ii)')';
  38.         if Num(2)==0
  39.         One2nintynine=One2nintynine+S(Num(1)-1);
  40.         else
  41.         One2nintynine=One2nintynine+S(Num(1)-1)+F1(Num(2));
  42.         end
  43.     end
  44. end
  45. Sum=Sum+One2nintynine;%1-99
  46. for aa=1:9
  47.     Sum=Sum+T(aa)*99+One2nintynine;%101-199,....901-999
  48.     Sum=Sum+T(aa)-3;%100,200,300....900
  49. end
  50. Sum=Sum+length('onethousand');
  51. toc
  52. Output=Sum;
  53. disp('此代码使用matlab编程')
  54. disp(['Problem17所用时间为',num2str(toc),'秒'])
  55. disp(['Problem17的答案为',num2str(Output)])
  56. end
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-2-14 14:56:18 | 显示全部楼层
  1. import time

  2. def letter_number(number):
  3.     '计算英文写数字number需要多少个字母,一千以内'
  4.     dict_letter = {
  5.         1:3,
  6.         2:3,
  7.         3:5,
  8.         4:4,
  9.         5:4,
  10.         6:3,
  11.         7:5,
  12.         8:5,
  13.         9:4,
  14.         10:3,
  15.         11:6,
  16.         12:6,
  17.         13:8,
  18.         14:8,
  19.         15:7,
  20.         16:7,
  21.         17:9,
  22.         18:8,
  23.         19:8,
  24.         20:6,
  25.         30:6,
  26.         40:5,
  27.         50:5,
  28.         60:5,
  29.         70:7,
  30.         80:6,
  31.         90:6,
  32.         100:10,
  33.         1000:11
  34.         }

  35.     if number in dict_letter:
  36.         return dict_letter[number]
  37.     if number > 20 and number < 100:
  38.         return dict_letter[(number // 10) * 10] + dict_letter[number % 10]
  39.     if number > 100 and number < 1000:
  40.         if number % 100 in dict_letter:
  41.             return dict_letter[100] +dict_letter[number // 100] + dict_letter[number % 100]
  42.         elif number % 100 == 0:
  43.             return dict_letter[100] + dict_letter[number // 100] - 3
  44.         else:
  45.             return dict_letter[100] + dict_letter[number // 100] + dict_letter[(number // 10 % 10) * 10] + dict_letter[number % 10]

  46. def letter_counts(number):
  47.     '计算英文写数字1到number共需要多少个字母'
  48.     count = 0
  49.     for i in range(1, number + 1):
  50.         count += letter_number(i)
  51.     return count

  52. start = time.clock()
  53. print(letter_counts(1000))
  54. end = time.clock()
  55. print('程序执行了%fs。' %(end - start))
复制代码

执行结果:
21124
程序执行了0.004624s。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-4-5 17:17:19 | 显示全部楼层
本帖最后由 JonTargaryen 于 2017-4-5 17:19 编辑
  1. #include <stdio.h>
  2. #include <string.h>

  3. #define MAX 10

  4. int main()
  5. {
  6.     char numbers[30][10] = {"and", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",\
  7.         "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",\
  8.         "hundred", "thousand", "tewnty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};  //[0]: and; [1-19]: 1-19; [20]: 100; [21]: 1000; [22-29]: 20-90
  9.     int i;
  10.     int n = 1000;
  11.     int count = 0;

  12.     int sum_1to9 = 0;
  13.     int sum_10to19 = 0;
  14.     int sum_20to99 = 0;
  15.     int sum_100to999 = 0;

  16.     for(i = 1; i <= 9; i++)
  17.     {
  18.         sum_1to9 += strlen(numbers[i]);
  19.     }

  20.     for(i = 10; i <= 19; i++)
  21.     {
  22.         sum_10to19 += strlen(numbers[i]);
  23.     }

  24.     for(i = 22; i <= 29; i++)
  25.     {
  26.         sum_20to99 += strlen(numbers[i]);
  27.     }
  28.     sum_20to99 = 10 * sum_20to99 + 8 * sum_1to9;

  29.     sum_100to999 = (999 - 99) * (strlen(numbers[20])) + (999 - 99 - 9) * (strlen(numbers[0]))  + 100 * sum_1to9 + 9 * (sum_1to9 + sum_10to19 + sum_20to99);

  30.     count = sum_1to9 + sum_10to19 + sum_20to99 + sum_100to999 + (strlen(numbers[1]) + strlen(numbers[21]));

  31.     printf("%d\n", count);

  32.     return 0;
  33. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-5 19:07:31 | 显示全部楼层
  1. def main():
  2.     numbers = {
  3.         1: len("one"),
  4.         2: len("two"),
  5.         3: len("three"),
  6.         4: len("four"),
  7.         5: len("five"),
  8.         6: len("six"),
  9.         7: len("seven"),
  10.         8: len("eight"),
  11.         9: len("nine"),
  12.         10: len("ten"),
  13.         11: len("eleven"),
  14.         12: len("twelve"),
  15.         13: len("thirteen"),
  16.         14: len("fourteen"),
  17.         15: len("fifteen"),
  18.         16: len("sixteen"),
  19.         17: len("seventeen"),
  20.         18: len("eighteen"),
  21.         19: len("nineteen"),
  22.         20: len("twenty"),
  23.         30: len("thirty"),
  24.         40: len("forty"),
  25.         50: len("fifty"),
  26.         60: len("sixty"),
  27.         70: len("seventy"),
  28.         80: len("eighty"),
  29.         90: len("ninety"),
  30.         100: len("hundred"),
  31.         1000: len("thousand")}

  32.     sum_1to9 = 0
  33.     sum_10to19 = 0
  34.     sum_20to99 = 0
  35.     sum_100to999 = 0

  36.     for i in range(1, 10):
  37.         sum_1to9 += numbers[i]

  38.     for i in  range(10, 20):
  39.         sum_10to19 += numbers[i]

  40.     for i in range(20, 100, 10):
  41.         sum_20to99 += numbers[i]
  42.     sum_20to99 = 10 * sum_20to99 + 8 * sum_1to9

  43.     sum_100to999 = (999 - 99) * numbers[100] + (999 - 99 - 9) * len("and")  + 100 * sum_1to9 + 9 * (sum_1to9 + sum_10to19 + sum_20to99)

  44.     count = sum_1to9 + sum_10to19 + sum_20to99 + sum_100to999 + numbers[1] + numbers[1000]

  45.     print(count)

  46. if __name__ == '__main__':
  47.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-13 15:22:38 | 显示全部楼层
然而你们都忽略了and吗?我算出来的是23581


  1. underTwenty = ''' one two three four five six seven eight nine
  2. ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen'''
  3. a = underTwenty.split('\n')
  4. units = str(a[0].replace(' ',''))
  5. teens = str(a[1].replace(' ',''))
  6. tens = ['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
  7. hundreds = ['','onehundredand','twohundredand','threehundredand','fourhundredand',\
  8.      'fivehundredand','sixhundredand','sevenhundredand','eighthundredand',\
  9.      'ninehundredand']
  10. add = 'and' * 9

  11. total = ''
  12. count = 0
  13. for each in hundreds:
  14.     for itens in range(10):
  15.         if not itens:
  16.             total += 10 * each + units
  17.         elif itens == 1:
  18.             total += 10 * each + teens + add
  19.         else:
  20.             total += 10 * (each + tens[itens]) + units + add

  21. total += 'onethousand'
  22. print(len(total))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-14 20:47:20 | 显示全部楼层
本帖最后由 Wave.LT 于 2018-12-14 20:48 编辑
  1. clear all;
  2. clc;
  3. tic
  4. s=0;
  5. t=input('请输入一个1-1000的数字\n');
  6. for a=1:t
  7. b=num2str(a);
  8. c=length(b);
  9. d=num2str(a);
  10. A={'zero','one','two','three','four','five','six','seven','eight','nine'};
  11. B={'ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty'};
  12. C={'ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'};
  13. D={'one hundred','two hundred','three hundred','four hundred','five hundred','six hundred','seven hundred','eight hundred','nine hundred'};
  14. if c==1
  15.    disp([num2str(a),' is ',char(A(a+1))]);
  16.    e=length(char(A(a+1)));
  17. end
  18. if c==2
  19.     if a>=10&&a<=20
  20.         disp([num2str(a),' is ',char(B(a-10+1))]);
  21.         e=length(char(B(a-10+1)));
  22.     elseif (a>20&&a<=99)&&(mod(a,10)~=0)
  23.         disp([num2str(a),' is ',char(C(str2double(d(1)))),' - ',char(A(str2double(d(2))+1))]);
  24.         e=length(char(C(str2double(d(1)))))+length(char(A(str2double(d(2))+1)));
  25.     elseif mod(a,10)==0
  26.         disp([num2str(a),' is ',char(C(str2double(d(1))))]);
  27.         e=length(char(C(str2double(d(1)))));
  28.     end
  29. end
  30. if c==3
  31.     if mod(a,100)==0
  32.         disp([num2str(a),' is ',char(D(str2double(d(1))))])
  33.         e=length(char(D(str2double(d(1)))))-1;
  34.     end
  35.     if mod(a,10)==0&&mod(a,100)~=0
  36.         disp([num2str(a),' is ',char(D(str2double(d(1)))),' and ',char(C(str2double(d(2))))]);
  37.         e=length(char(D(str2double(d(1)))))-1+3+length(char(C(str2double(d(2)))));
  38.     end
  39.     if mod(a,10)~=0
  40.         if d(2)=='0'
  41.             disp([num2str(a),' is ',char(D(str2double(d(1)))),' and ',char(A(str2double(d(3))+1))]);
  42.             e=length(char(D(str2double(d(1)))))-1+3+length(char(A(str2double(d(3))+1)));
  43.         elseif d(2)=='1'
  44.             disp([num2str(a),' is ',char(D(str2double(d(1)))),' and ',char(B(str2double(d(3))+1))]);
  45.             e=length(char(D(str2double(d(1)))))-1+3+length(char(B(str2double(d(3))+1)));
  46.         else
  47.             disp([num2str(a),' is ',char(D(str2double(d(1)))),' and ',char(C(str2double(d(2)))),' ',char(A(str2double(d(3))+1))]);
  48.             e=length(char(D(str2double(d(1)))))-1+3+length(char(A(str2double(d(3))+1)))+length(char(C(str2double(d(2)))));
  49.         end
  50.     end
  51. end
  52. if a==1000
  53.     disp([num2str(a),' is one thousand']);
  54.     e=length('one')+length('thousand');
  55. end
  56. disp(['the length of the number ',num2str(a),' is ',num2str(e)]);
  57. s=s+e;
  58. end
  59. disp(['the number of the used letter is ',num2str(s)]);
  60. toc
复制代码

当输入截止数字1000时,其结果为 21124   并且每个数字都能转化成相应的英语且能计算出每个数字所用字母的个数。


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-29 17:35:28 | 显示全部楼层
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


void main()
{
        int i;
        int nums = 0;//统计字母的个数
        int nums_1_99 = 0;//统计1-99字母个数
        char strs[100000] = "\0";
        char first_ten[11][10] = { "","one","two","three","four","five","six","seven","eight","nine","ten" };
        char eleven_nineteen[9][10] = { "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen" };
        char twenty_ninetys[8][10] = { "twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
        for (i = 0; i < 11; i++)//1-10
        {
                strcat(strs, first_ten[i]);
        }
        for (i = 0; i < 9; i++)//11-19
        {
                strcat(strs, eleven_nineteen[i]);
        }
        for (i = 0; i < 8; i++)//20-99
        {
                for (int j = 0; j < 10; j++)
                {
                        strcat(strs, twenty_ninetys[i]);
                        strcat(strs, first_ten[j]);
                }
        }
        nums_1_99 = strlen(strs);//1-99的字母个数
        nums += nums_1_99;
        for (i = 100; i <= 999; i++)
        {
                nums += strlen(first_ten[i / 100]);
                nums += strlen("hundred");
                nums += 3;
        }
        nums += nums_1_99 * 9;//每一个百位重复十位个位
        nums += strlen("onethousand");
        nums -= 3 * 9;//整百多加了and,需要去掉
       
       
        printf("一共需要%d个字母\n", nums);


        system("pause");
}
输出结果:
一共需要21124个字母
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-9 17:11:18 | 显示全部楼层
  1. '''如果用英文写出数字 1 到 5: one, two, three, four, five, 那么一共需要 3 + 3 + 5 + 4 + 4 = 19 个字母。
  2. 如果数字 1 到 1000(包含 1000)用英文写出,那么一共需要多少个字母?
  3. 注意: 空格和连字符不算在内。例如,342 (three hundred and forty-two)包含 23 个字母; 115 (one hundred and fifteen)包含 20 个字母。"and" 的使用与英国标准一致。  
  4. '''

  5. letters_numbers_dict = dict()
  6. letters_numbers_dict[0] = ""
  7. letters_numbers_dict[1] = "one"
  8. letters_numbers_dict[2] = "two"
  9. letters_numbers_dict[3] = "three"
  10. letters_numbers_dict[4] = "four"
  11. letters_numbers_dict[5] = "five"
  12. letters_numbers_dict[6] = "six"
  13. letters_numbers_dict[7] = "seven"
  14. letters_numbers_dict[8] = "eight"
  15. letters_numbers_dict[9] = "nine"

  16. letters_numbers_dict[10] = "ten"
  17. letters_numbers_dict[11] = "eleven"
  18. letters_numbers_dict[12] = "twelve"
  19. letters_numbers_dict[13] = "thirteen"
  20. letters_numbers_dict[14] = "fourteen"
  21. letters_numbers_dict[15] = "fifteen"
  22. letters_numbers_dict[16] = "sixteen"
  23. letters_numbers_dict[17] = "seventeen"
  24. letters_numbers_dict[18] = "eighteen"
  25. letters_numbers_dict[19] = "nineteen"

  26. letters_numbers_dict[20] = "twenty"
  27. letters_numbers_dict[30] = "thirty"
  28. letters_numbers_dict[40] = "forty"
  29. letters_numbers_dict[50] = "fifty"
  30. letters_numbers_dict[60] = "sixty"
  31. letters_numbers_dict[70] = "seventy"
  32. letters_numbers_dict[80] = "eighty"
  33. letters_numbers_dict[90] = "ninety"

  34. letters_numbers_dict[100] = "hundred"
  35. letters_numbers_dict[1000] = "thousand"
  36. letters_numbers_dict["and"] = "and"


  37. words_list = []
  38. Words_list = []

  39. def less_10(count):
  40.     words_list.append(len(letters_numbers_dict[int(count%10)]))
  41.     Words_list.append(letters_numbers_dict[int(count%10)])
  42. def between_10_100(count):
  43.     if count % 100 >= 10 and count % 100 <= 19:
  44.         words_list.append(len(letters_numbers_dict[count % 100]))
  45.         Words_list.append(letters_numbers_dict[count % 100])
  46.     elif count % 100 >= 20:
  47.         words_list.append(len(letters_numbers_dict[count % 100 - count % 10])+len(letters_numbers_dict[count % 10]))
  48.         Words_list.append(letters_numbers_dict[count % 100 - count % 10] + letters_numbers_dict[count % 10])

  49. def between_100_1000(count):
  50.     words_list.append(len(letters_numbers_dict[int((count % 1000)/100)]))
  51.     words_list.append(len(letters_numbers_dict[100]))
  52.     words_list.append(len(letters_numbers_dict["and"]))
  53.     Words_list.append(letters_numbers_dict[int((count % 1000)/100)])
  54.     Words_list.append(letters_numbers_dict[100])
  55.     Words_list.append(letters_numbers_dict["and"])
  56.     if count % 100 == 0 :
  57.         words_list.remove(3)
  58.         Words_list.remove("and")
  59.     else:
  60.         if count % 100 <= 9 and count % 100 >= 1:
  61.             less_10(count)
  62.         elif count % 100 >= 10 and count % 100 < 100:
  63.             between_10_100(count)
  64.     if count == 1000:
  65.         words_list.append(len(letters_numbers_dict[1]))
  66.         words_list.remove(len(letters_numbers_dict[100]))
  67.         words_list.append(len(letters_numbers_dict[1000]))
  68.         Words_list.append("one")
  69.         Words_list.remove("hundred")
  70.         Words_list.append(letters_numbers_dict[1000])

  71. def calculate(numbers):
  72.     for i in range(numbers):
  73.         global count
  74.         count = i + 1
  75.         if count < 10:
  76.             less_10(count)
  77.         elif count < 100 and count >= 10:
  78.             between_10_100(count)
  79.         elif count >= 100 and count <= 1000:
  80.             between_100_1000(count)
  81.     #print(words_list)
  82.     #print(Words_list)
  83.     sum = 0
  84.     for each in words_list:
  85.         sum += each
  86.     print("从1到%d的所有数字的英文字母有%d个" %(numbers,sum))

  87. start_calculate = time.time()
  88. calculate(1000)
  89. time_calculate = time.time() - start_calculate
  90. print("%f秒" %time_calculate)
复制代码



从1到1000的所有数字的英文字母有21124个
0.002999秒
此代码可以显示出所有长度和英文
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 11:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表