欧拉计划 发表于 2015-4-21 16:45:58

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

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" 的使用与英国标准一致。

愤怒的大头菇 发表于 2016-8-25 23:22:00

know={
'0':0,
'1':3,
'2':3,
'3':5,
'4':4,
'5':4,
'6':3,
'7':5,
'8':5,
'9':4,
'10':3,
'11':6,
'12':6,
'13':8,
'14':8,
'15':7,
'16':7,
'17':9,
'18':8,
'19':8,
'20':6,
'30':6,
'40':6,
'50':5,
'60':5,
'70':7,
'80':6,
'90':6,}
total = 0
for i in range(1,1001):
      n = str(i)
      if n in know:
            total += know
      elif len(n) == 2 and n != '1' and n!='0':
            total += know+'0'] + know]
      elif len(n) == 3:
            if n == '0' and n == '0':
                  total += know] + 7
            elif n != '0' and n == '0':
                  total += know]+10 +know]
            elif n == '0' and n != '0':
                  total += know] + 10 + know]
            else:
                  if n in know:
                        total += know] + 10 + know]
                  else:
                        total += know]+10+know+'0'] + know]
      else:
            total += 11#加上one thousand
print(total)
不知道对不对 算出来是21224

Plusenxue 发表于 2016-8-27 14:02:56

a = 'onetwothreefourfivesixseveneightnine'
a2 = 'teneleventwelvethirteenfourteenfifteensixteenseventeeneighteennineteen'
b = ['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninty']
c = ['','onehundredand','twohundredand','threehundredand','fourhundredand',\
   'fivehundredand','sixhundredand','sevenhundredand','eighthundredand',\
   'ninehundredand']

n = ''
count = 0
for i in c:
    for j in range(10):
      if j == 0:
            n = n+i*10+a
      if j == 1:
            n = n+i*10+a2

      if j != 0 and j != 1:
            n = n+i*10+b*10+a

n = n+'onethousand'
print(len(n))

jerryxjr1220 发表于 2016-10-11 14:35:40

愤怒的大头菇 发表于 2016-8-25 23:22
不知道对不对 算出来是21224

你的算法是对的,但是Forty是5个字母,不是Fourty
所以你的答案多了100
正确的应该是21124
know={
'0':0,
'1':3,
'2':3,
'3':5,
'4':4,
'5':4,
'6':3,
'7':5,
'8':5,
'9':4,
'10':3,
'11':6,
'12':6,
'13':8,
'14':8,
'15':7,
'16':7,
'17':9,
'18':8,
'19':8,
'20':6,
'30':6,
'40':5,
'50':5,
'60':5,
'70':7,
'80':6,
'90':6,
'100':10,
'200':10,
'300':12,
'400':11,
'500':11,
'600':10,
'700':12,
'800':12,
'900':11}
total = 0
for i in range(1,1001):
        n = str(i)
        if n in know:
                total += know
        elif i>100 and i<1000:
                total += know]+10
                if str(int(n)) in know:
                        total += know))]
                else:
                        total += know+'0']+know]
        elif i>20 and i<100 and n not in know:
                total += know+'0']+know]
        else:
                total += 11
print(total)

776667 发表于 2016-10-16 15:06:22

first_ten = ['one','two','three','four','five','six','seven','eight','nine','ten']
second_ten = ['eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
over_twenty = ['twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']

def letter_trans(number):
    result = []
    if number == 1000:
      return 'one thousand'
    if number >= 100:
      result.append(first_ten)-1])
      result.append('hundred')
      number = int(str(number))
      if number:
            result.append('and')
      else:
            return ' '.join(result)
    if number >= 20:
      result.append(over_twenty)-2])
      number = int(str(number))
      if number:
            result.append(first_ten)
      return ' '.join(result)
    if 11 <= number <= 19:
      result.append(second_ten)
      return ' '.join(result)
    if number <= 10:
      result.append(first_ten)
      return ' '.join(result)

def euler(x):
    list_x = []
    for i in range(1,x+1):
      list_x.append(len())
    return sum(list_x)

if __name__ == '__main__':
    print(euler(1000))

愤怒的大头菇 发表于 2016-11-16 11:38:03

jerryxjr1220 发表于 2016-10-11 14:35
你的算法是对的,但是Forty是5个字母,不是Fourty
所以你的答案多了100
正确的应该是21124

谢谢,终于知道是哪里错了

芒果加黄桃 发表于 2017-1-10 15:44:34

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

def euler017(N):
    count = 0
    for i in range(1,N+1):
      l_result=[]
      getChars(i,l_result)
      #print(l_result)
      count += sum(len(ch) for ch in l_result)
    print(count)
            
def getChars(n,l_result):
    if n == 100 or n == 1000:
      l_result.append('one')
      l_result.append(dic.get(n))
    elif dic.get(n):
      l_result.append(dic.get(n))
    else:   
      if n>100:
            lenth = len(str(n))
            tmp = int((n - n%(10**(lenth-1)))/10**(lenth-1))
            l_result.append(dic.get(tmp))
            l_result.append(dic.get(10**(lenth-1)))
            n = n%10**(lenth-1)
            if n:
                l_result.append('and')
                getChars(n,l_result)
      else:
            tmp = n - n%10
            l_result.append(dic.get(int(n - n%10)))
            l_result.append(dic.get(int(n%10)))
            
if __name__ == '__main__':
    start = time()
    euler017(1000)
    print('cost %.6f sec' % (time() - start))

渡风 发表于 2017-1-18 11:20:21

Plusenxue 发表于 2016-8-27 14:02


大哥你的ninety,少了个e,害死我了,我去

渡风 发表于 2017-1-18 11:27:41

此代码使用matlab编程
Problem17所用时间为0.014651秒
Problem17的答案为21124
%题目17:用英文写出1到1000的所有数字需要多少个字母?
function Output=Problem17(Input)
tic
if nargin==0
    Input=1000;
end
One2nintynine=0;%1到99的值
Sum=0;
a = {'one','two','three','four','five','six','seven','eight','nine','ten'};
a2 = {'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'};
b = {'twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'};
c = {'onehundredand','twohundredand','threehundredand','fourhundredand',...
   'fivehundredand','sixhundredand','sevenhundredand','eighthundredand',...
   'ninehundredand'};
F1=zeros(1,length(a));
F2=zeros(1,length(a2));
S=zeros(1,length(b));
T=zeros(1,length(c));
for ii=1:length(a)
    F1(ii)=length(a{ii});
end
for jj=1:length(a2)
    F2(jj)=length(a2{jj});
end
for kk=1:length(b)
    S(kk)=length(b{kk});
end
for ll=1:length(c)
    T(ll)=length(c{ll});
end
for ii=1:Input/10-1%99
    if 1<=ii&&ii<=10
      One2nintynine=One2nintynine+F1(ii);
    elseif 11<=ii&&ii<=19
      One2nintynine=One2nintynine+F2(ii-10);
    else
      Num=str2num(num2str(ii)')';
      if Num(2)==0
      One2nintynine=One2nintynine+S(Num(1)-1);
      else
      One2nintynine=One2nintynine+S(Num(1)-1)+F1(Num(2));
      end
    end
end
Sum=Sum+One2nintynine;%1-99
for aa=1:9
    Sum=Sum+T(aa)*99+One2nintynine;%101-199,....901-999
    Sum=Sum+T(aa)-3;%100,200,300....900
end
Sum=Sum+length('onethousand');
toc
Output=Sum;
disp('此代码使用matlab编程')
disp(['Problem17所用时间为',num2str(toc),'秒'])
disp(['Problem17的答案为',num2str(Output)])
end

FlySelf 发表于 2017-2-14 14:56:18

import time

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

    if number in dict_letter:
      return dict_letter
    if number > 20 and number < 100:
      return dict_letter[(number // 10) * 10] + dict_letter
    if number > 100 and number < 1000:
      if number % 100 in dict_letter:
            return dict_letter +dict_letter + dict_letter
      elif number % 100 == 0:
            return dict_letter + dict_letter - 3
      else:
            return dict_letter + dict_letter + dict_letter[(number // 10 % 10) * 10] + dict_letter

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

start = time.clock()
print(letter_counts(1000))
end = time.clock()
print('程序执行了%fs。' %(end - start))

执行结果:
21124
程序执行了0.004624s。

JonTargaryen 发表于 2017-4-5 17:17:19

本帖最后由 JonTargaryen 于 2017-4-5 17:19 编辑

#include <stdio.h>
#include <string.h>

#define MAX 10

int main()
{
    char numbers = {"and", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",\
      "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",\
      "hundred", "thousand", "tewnty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};//: and; : 1-19; : 100; : 1000; : 20-90
    int i;
    int n = 1000;
    int count = 0;

    int sum_1to9 = 0;
    int sum_10to19 = 0;
    int sum_20to99 = 0;
    int sum_100to999 = 0;

    for(i = 1; i <= 9; i++)
    {
      sum_1to9 += strlen(numbers);
    }

    for(i = 10; i <= 19; i++)
    {
      sum_10to19 += strlen(numbers);
    }

    for(i = 22; i <= 29; i++)
    {
      sum_20to99 += strlen(numbers);
    }
    sum_20to99 = 10 * sum_20to99 + 8 * sum_1to9;

    sum_100to999 = (999 - 99) * (strlen(numbers)) + (999 - 99 - 9) * (strlen(numbers))+ 100 * sum_1to9 + 9 * (sum_1to9 + sum_10to19 + sum_20to99);

    count = sum_1to9 + sum_10to19 + sum_20to99 + sum_100to999 + (strlen(numbers) + strlen(numbers));

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

    return 0;
}

JonTargaryen 发表于 2017-4-5 19:07:31

JonTargaryen 发表于 2017-4-5 17:17


def main():
    numbers = {
      1: len("one"),
      2: len("two"),
      3: len("three"),
      4: len("four"),
      5: len("five"),
      6: len("six"),
      7: len("seven"),
      8: len("eight"),
      9: len("nine"),
      10: len("ten"),
      11: len("eleven"),
      12: len("twelve"),
      13: len("thirteen"),
      14: len("fourteen"),
      15: len("fifteen"),
      16: len("sixteen"),
      17: len("seventeen"),
      18: len("eighteen"),
      19: len("nineteen"),
      20: len("twenty"),
      30: len("thirty"),
      40: len("forty"),
      50: len("fifty"),
      60: len("sixty"),
      70: len("seventy"),
      80: len("eighty"),
      90: len("ninety"),
      100: len("hundred"),
      1000: len("thousand")}

    sum_1to9 = 0
    sum_10to19 = 0
    sum_20to99 = 0
    sum_100to999 = 0

    for i in range(1, 10):
      sum_1to9 += numbers

    for i inrange(10, 20):
      sum_10to19 += numbers

    for i in range(20, 100, 10):
      sum_20to99 += numbers
    sum_20to99 = 10 * sum_20to99 + 8 * sum_1to9

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

    count = sum_1to9 + sum_10to19 + sum_20to99 + sum_100to999 + numbers + numbers

    print(count)

if __name__ == '__main__':
    main()

BngThea 发表于 2017-9-13 15:22:38

然而你们都忽略了and吗?我算出来的是23581


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

total = ''
count = 0
for each in hundreds:
    for itens in range(10):
      if not itens:
            total += 10 * each + units
      elif itens == 1:
            total += 10 * each + teens + add
      else:
            total += 10 * (each + tens) + units + add

total += 'onethousand'
print(len(total))

Wave.LT 发表于 2018-12-14 20:47:20

本帖最后由 Wave.LT 于 2018-12-14 20:48 编辑

clear all;
clc;
tic
s=0;
t=input('请输入一个1-1000的数字\n');
for a=1:t
b=num2str(a);
c=length(b);
d=num2str(a);
A={'zero','one','two','three','four','five','six','seven','eight','nine'};
B={'ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty'};
C={'ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'};
D={'one hundred','two hundred','three hundred','four hundred','five hundred','six hundred','seven hundred','eight hundred','nine hundred'};
if c==1
   disp();
   e=length(char(A(a+1)));
end
if c==2
    if a>=10&&a<=20
      disp();
      e=length(char(B(a-10+1)));
    elseif (a>20&&a<=99)&&(mod(a,10)~=0)
      disp();
      e=length(char(C(str2double(d(1)))))+length(char(A(str2double(d(2))+1)));
    elseif mod(a,10)==0
      disp();
      e=length(char(C(str2double(d(1)))));
    end
end
if c==3
    if mod(a,100)==0
      disp()
      e=length(char(D(str2double(d(1)))))-1;
    end
    if mod(a,10)==0&&mod(a,100)~=0
      disp();
      e=length(char(D(str2double(d(1)))))-1+3+length(char(C(str2double(d(2)))));
    end
    if mod(a,10)~=0
      if d(2)=='0'
            disp();
            e=length(char(D(str2double(d(1)))))-1+3+length(char(A(str2double(d(3))+1)));
      elseif d(2)=='1'
            disp();
            e=length(char(D(str2double(d(1)))))-1+3+length(char(B(str2double(d(3))+1)));
      else
            disp();
            e=length(char(D(str2double(d(1)))))-1+3+length(char(A(str2double(d(3))+1)))+length(char(C(str2double(d(2)))));
      end
    end
end
if a==1000
    disp();
    e=length('one')+length('thousand');
end
disp(['the length of the number ',num2str(a),' is ',num2str(e)]);
s=s+e;
end
disp(['the number of the used letter is ',num2str(s)]);
toc
当输入截止数字1000时,其结果为 21124   并且每个数字都能转化成相应的英语且能计算出每个数字所用字母的个数。


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


        system("pause");
}
输出结果:
一共需要21124个字母

有马_冬巳 发表于 2020-10-9 17:11:18

'''如果用英文写出数字 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" 的使用与英国标准一致。
'''

letters_numbers_dict = dict()
letters_numbers_dict = ""
letters_numbers_dict = "one"
letters_numbers_dict = "two"
letters_numbers_dict = "three"
letters_numbers_dict = "four"
letters_numbers_dict = "five"
letters_numbers_dict = "six"
letters_numbers_dict = "seven"
letters_numbers_dict = "eight"
letters_numbers_dict = "nine"

letters_numbers_dict = "ten"
letters_numbers_dict = "eleven"
letters_numbers_dict = "twelve"
letters_numbers_dict = "thirteen"
letters_numbers_dict = "fourteen"
letters_numbers_dict = "fifteen"
letters_numbers_dict = "sixteen"
letters_numbers_dict = "seventeen"
letters_numbers_dict = "eighteen"
letters_numbers_dict = "nineteen"

letters_numbers_dict = "twenty"
letters_numbers_dict = "thirty"
letters_numbers_dict = "forty"
letters_numbers_dict = "fifty"
letters_numbers_dict = "sixty"
letters_numbers_dict = "seventy"
letters_numbers_dict = "eighty"
letters_numbers_dict = "ninety"

letters_numbers_dict = "hundred"
letters_numbers_dict = "thousand"
letters_numbers_dict["and"] = "and"


words_list = []
Words_list = []

def less_10(count):
    words_list.append(len(letters_numbers_dict))
    Words_list.append(letters_numbers_dict)
def between_10_100(count):
    if count % 100 >= 10 and count % 100 <= 19:
      words_list.append(len(letters_numbers_dict))
      Words_list.append(letters_numbers_dict)
    elif count % 100 >= 20:
      words_list.append(len(letters_numbers_dict)+len(letters_numbers_dict))
      Words_list.append(letters_numbers_dict + letters_numbers_dict)

def between_100_1000(count):
    words_list.append(len(letters_numbers_dict))
    words_list.append(len(letters_numbers_dict))
    words_list.append(len(letters_numbers_dict["and"]))
    Words_list.append(letters_numbers_dict)
    Words_list.append(letters_numbers_dict)
    Words_list.append(letters_numbers_dict["and"])
    if count % 100 == 0 :
      words_list.remove(3)
      Words_list.remove("and")
    else:
      if count % 100 <= 9 and count % 100 >= 1:
            less_10(count)
      elif count % 100 >= 10 and count % 100 < 100:
            between_10_100(count)
    if count == 1000:
      words_list.append(len(letters_numbers_dict))
      words_list.remove(len(letters_numbers_dict))
      words_list.append(len(letters_numbers_dict))
      Words_list.append("one")
      Words_list.remove("hundred")
      Words_list.append(letters_numbers_dict)

def calculate(numbers):
    for i in range(numbers):
      global count
      count = i + 1
      if count < 10:
            less_10(count)
      elif count < 100 and count >= 10:
            between_10_100(count)
      elif count >= 100 and count <= 1000:
            between_100_1000(count)
    #print(words_list)
    #print(Words_list)
    sum = 0
    for each in words_list:
      sum += each
    print("从1到%d的所有数字的英文字母有%d个" %(numbers,sum))

start_calculate = time.time()
calculate(1000)
time_calculate = time.time() - start_calculate
print("%f秒" %time_calculate)


从1到1000的所有数字的英文字母有21124个
0.002999秒
此代码可以显示出所有长度和英文
页: [1]
查看完整版本: 题目17:用英文写出1到1000的所有数字需要多少个字母?