鱼C论坛

 找回密码
 立即注册
查看: 5137|回复: 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 | 显示全部楼层
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[n]
      elif len(n) == 2 and n[0] != '1' and n[1]!='0':
            total += know[n[0]+'0'] + know[n[1]]
      elif len(n) == 3:
            if n[1] == '0' and n[2] == '0':
                  total += know[n[0]] + 7
            elif n[1] != '0' and n[2] == '0':
                  total += know[n[0]]+10 +know[n[1:]]
            elif n[1] == '0' and n[2] != '0':
                  total += know[n[0]] + 10 + know[n[2]]
            else:
                  if n[1:] in know:
                        total += know[n[0]] + 10 + know[n[1:]]
                  else:
                        total += know[n[0]]+10+know[n[1]+'0'] + know[n[2]]
      else:
            total += 11  #加上one thousand
print(total)
不知道对不对 算出来是21224
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[j]*10+a

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

使用道具 举报

发表于 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[n]
        elif i>100 and i<1000:
                total += know[n[0]]+10
                if str(int(n[1:3])) in know:
                        total += know[str(int(n[1:3]))]
                else:
                        total += know[n[1]+'0']+know[n[2]]
        elif i>20 and i<100 and n not in know:
                total += know[n[0]+'0']+know[n[1]]
        else:
                total += 11
print(total)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[int(str(number)[0])-1])
        result.append('hundred')
        number = int(str(number)[1:])
        if number:
            result.append('and')
        else:
            return ' '.join(result)
    if number >= 20:
        result.append(over_twenty[int(str(number)[0])-2])
        number = int(str(number)[1:])
        if number:
            result.append(first_ten[number-1])
        return ' '.join(result)
    if 11 <= number <= 19:
        result.append(second_ten[number-11])
        return ' '.join(result)
    if number <= 10:
        result.append(first_ten[number-1])
        return ' '.join(result)

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

if __name__ == '__main__':
    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 | 显示全部楼层
# 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))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

使用道具 举报

发表于 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 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[number]
    if number > 20 and number < 100:
        return dict_letter[(number // 10) * 10] + dict_letter[number % 10]
    if number > 100 and number < 1000:
        if number % 100 in dict_letter:
            return dict_letter[100] +dict_letter[number // 100] + dict_letter[number % 100]
        elif number % 100 == 0:
            return dict_letter[100] + dict_letter[number // 100] - 3
        else:
            return dict_letter[100] + dict_letter[number // 100] + dict_letter[(number // 10 % 10) * 10] + dict_letter[number % 10]

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。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 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[30][10] = {"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"};  //[0]: and; [1-19]: 1-19; [20]: 100; [21]: 1000; [22-29]: 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[i]);
    }

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

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

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

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

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

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

使用道具 举报

发表于 2017-4-5 19:07:31 | 显示全部楼层
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[i]

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

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

    sum_100to999 = (999 - 99) * numbers[100] + (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[1] + numbers[1000]

    print(count)

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

使用道具 举报

发表于 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[0].replace(' ',''))
teens = str(a[1].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[itens]) + units + add

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

使用道具 举报

发表于 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([num2str(a),' is ',char(A(a+1))]);
   e=length(char(A(a+1)));
end
if c==2
    if a>=10&&a<=20
        disp([num2str(a),' is ',char(B(a-10+1))]);
        e=length(char(B(a-10+1)));
    elseif (a>20&&a<=99)&&(mod(a,10)~=0)
        disp([num2str(a),' is ',char(C(str2double(d(1)))),' - ',char(A(str2double(d(2))+1))]);
        e=length(char(C(str2double(d(1)))))+length(char(A(str2double(d(2))+1)));
    elseif mod(a,10)==0
        disp([num2str(a),' is ',char(C(str2double(d(1))))]);
        e=length(char(C(str2double(d(1)))));
    end
end
if c==3
    if mod(a,100)==0
        disp([num2str(a),' is ',char(D(str2double(d(1))))])
        e=length(char(D(str2double(d(1)))))-1;
    end
    if mod(a,10)==0&&mod(a,100)~=0
        disp([num2str(a),' is ',char(D(str2double(d(1)))),' and ',char(C(str2double(d(2))))]);
        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([num2str(a),' is ',char(D(str2double(d(1)))),' and ',char(A(str2double(d(3))+1))]);
            e=length(char(D(str2double(d(1)))))-1+3+length(char(A(str2double(d(3))+1)));
        elseif d(2)=='1'
            disp([num2str(a),' is ',char(D(str2double(d(1)))),' and ',char(B(str2double(d(3))+1))]);
            e=length(char(D(str2double(d(1)))))-1+3+length(char(B(str2double(d(3))+1)));
        else
            disp([num2str(a),' is ',char(D(str2double(d(1)))),' and ',char(C(str2double(d(2)))),' ',char(A(str2double(d(3))+1))]);
            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([num2str(a),' is one thousand']);
    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   并且每个数字都能转化成相应的英语且能计算出每个数字所用字母的个数。


想知道小甲鱼最近在做啥?请访问 -> 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 到 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[0] = ""
letters_numbers_dict[1] = "one"
letters_numbers_dict[2] = "two"
letters_numbers_dict[3] = "three"
letters_numbers_dict[4] = "four"
letters_numbers_dict[5] = "five"
letters_numbers_dict[6] = "six"
letters_numbers_dict[7] = "seven"
letters_numbers_dict[8] = "eight"
letters_numbers_dict[9] = "nine"

letters_numbers_dict[10] = "ten"
letters_numbers_dict[11] = "eleven"
letters_numbers_dict[12] = "twelve"
letters_numbers_dict[13] = "thirteen"
letters_numbers_dict[14] = "fourteen"
letters_numbers_dict[15] = "fifteen"
letters_numbers_dict[16] = "sixteen"
letters_numbers_dict[17] = "seventeen"
letters_numbers_dict[18] = "eighteen"
letters_numbers_dict[19] = "nineteen"

letters_numbers_dict[20] = "twenty"
letters_numbers_dict[30] = "thirty"
letters_numbers_dict[40] = "forty"
letters_numbers_dict[50] = "fifty"
letters_numbers_dict[60] = "sixty"
letters_numbers_dict[70] = "seventy"
letters_numbers_dict[80] = "eighty"
letters_numbers_dict[90] = "ninety"

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


words_list = []
Words_list = []

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

def between_100_1000(count):
    words_list.append(len(letters_numbers_dict[int((count % 1000)/100)]))
    words_list.append(len(letters_numbers_dict[100]))
    words_list.append(len(letters_numbers_dict["and"]))
    Words_list.append(letters_numbers_dict[int((count % 1000)/100)])
    Words_list.append(letters_numbers_dict[100])
    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[1]))
        words_list.remove(len(letters_numbers_dict[100]))
        words_list.append(len(letters_numbers_dict[1000]))
        Words_list.append("one")
        Words_list.remove("hundred")
        Words_list.append(letters_numbers_dict[1000])

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秒
此代码可以显示出所有长度和英文
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-17 03:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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