²é¿´: 1702  |  »Ø¸´: 4
µ±Ç°Ö»ÏÔʾÂú×ãÖ¸¶¨Ìõ¼þµÄ»ØÌû£¬µã»÷ÕâÀï²é¿´±¾»°ÌâµÄËùÓлØÌû

libralibra

ÖÁ×ðľ³æ (ÖøÃûдÊÖ)

æôÆï½«¾ü

[½»Á÷] Euler Project Q17. Å·À­¹¤³ÌµÚÊ®ÆßÌâ ÒÑÓÐ3È˲ÎÓë

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µÄÊý×Ö±»Ð´³Éµ¥´ÊÐÎʽ,Ò»¹²Ê¹ÓöàÉÙ×Öĸ?

×¢Òâ:²»Òª¼ÆËã¿Õ¸ñ,Á¬×Ö·û.ÀýÈç343 (three hundred and forty-two)°üº¬23¸ö×Öĸ,115 (one hundred and fifteen) °üº¬20¸ö×Öĸ. Êý×Öתµ¥´Êʱ"and"Ó÷¨Ê¹ÓÃÓ¢¹ú¸ñʽ.
»Ø¸´´ËÂ¥

» ²ÂÄãϲ»¶

» ±¾Ö÷ÌâÏà¹Ø¼ÛÖµÌùÍÆ¼ö£¬¶ÔÄúͬÑùÓаïÖú:

matlab/VB/python/c++/Javaд³ÌÐòÇë·¢QQÓʼþ:790404545@qq.com
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

wangww2011

ľ³æ (ÖøÃûдÊÖ)

¡ï ¡ï ¡ï ¡ï ¡ï ¡ï
Сľ³æ(½ð±Ò+0.5):¸ø¸öºì°ü£¬Ð»Ð»»ØÌû
΢³¾¡¢ÃÎÏë(½ð±Ò+5): лл²ÎÓ룡 2011-05-29 16:32:11
ÕâÒ»ÌâºÜÎÞÁİ¢
½á¹û
CODE:
21124
elapsed time=0.000000 seconds.

´úÂë
CODE:
#include
#include
#include

#define TIMERSTART clock_t start_time,stop_time;double elapsed_time;start_time = clock();
#define TIMERSTOP stop_time = clock();elapsed_time=(double)(stop_time-start_time)/CLOCKS_PER_SEC;printf("elapsed time=%f seconds.\n",elapsed_time);

static int bufa[]={4,3,3,5,4,4,3,5,5,4,//zero, one, ..., nine,
                  3,6,6,8,8,7,7,9,8,8 };// ten, eleven, ..., nineteen
static int bufc[]={0,0,6,6,5,5,5,7,6,6}; //0, 0, twenty, thirty, ..., ninety

int count(int n){
  if(n<20)return bufa[n];
  if(n<100) {
    if(n%10==0) return bufc[n/10];
    else
      return bufc[n/10]+bufa[n%10];
  }
  if(n<1000) {
    if(n%100==0)
      return bufa[n/100]+7;
    else
      return bufa[n/100]+count(n%100)+10;
  }else if(n==1000) return 11;

  return -1;
}

int euler17(int n){
  int i,sum=0;
  
  for(i=1;i     sum+=count(i);
  
  return sum;
}


int main(void){
  
TIMERSTART;

printf("%d\n",euler17(1000));
  
TIMERSTOP;

  return 0;
}

5Â¥2011-05-29 11:16:25
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
²é¿´È«²¿ 5 ¸ö»Ø´ð

libralibra

ÖÁ×ðľ³æ (ÖøÃûдÊÖ)

æôÆï½«¾ü

¡ï ¡ï ¡ï ¡ï ¡ï
΢³¾¡¢ÃÎÏë(½ð±Ò+5): лл²ÎÓ룡 2011-05-29 16:31:31
Õâ¸öÌâͳ¼ÆºÜ¼òµ¥,ÉÔÓÐÄѶȾÍÊÇÊý×ֱ䵥´ÊµÄËã·¨

matlab code
CODE:
%% How many letters would be needed to write all the numbers in words from 1 to 1000?
function result = euler17()
tic;
numstr = '';
for i=1:1000
    numstr = [numstr,' ',num2words(i)];
end
numstr = strrep(numstr,' ','');
numstr = strtrim(numstr);
result = length(numstr);
toc;
end

%% write a number in words
% called by euler 17
function result = num2words(num)
key = [1:20,30:10:90];
value = {'one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve', ...
        'thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen', ...
        'twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'};
numlist = containers.Map(1,'one'); % must add a value first, then map can hold the right key type
for i=2:length(key)
    numlist(key(i)) = value{i};
end
result = '';
switch length(num2str(num))
    case 4
        if mod(num,1000)==0
            result = [result,numlist(fix(num/1000)),' thousand',num2words(rem(num,1000))];
        else
            result = [result,numlist(fix(num/1000)),' thousand and ',num2words(rem(num,1000))];
        end
    case 3
        if mod(num,100)==0
            result = [result,numlist(fix(num/100)),' hundred',num2words(rem(num,100))];
        else
            result = [result,numlist(fix(num/100)),' hundred and ',num2words(rem(num,100))];
        end
    case 2
        if fix(num/10)==1
            result = [result,numlist(num)];
        else
            result = [result,numlist(fix(num/10)*10),' ',num2words(rem(num,10))];
        end
    case 1
        if num==0
            result = '';
        else
            result = [result,numlist(num)];
        end
    otherwise
        result = '';
end
end

matlabЧÂÊ»¹ÊDz»¸ß
CODE:
% Elapsed time is 6.267496 seconds.
% ans =
%        21124

matlab/VB/python/c++/Javaд³ÌÐòÇë·¢QQÓʼþ:790404545@qq.com
2Â¥2011-05-28 03:54:36
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

ÄäÃû

Óû§×¢Ïú (СÓÐÃûÆø)

¡ï ¡ï ¡ï ¡ï ¡ï ¡ï
Сľ³æ(½ð±Ò+0.5):¸ø¸öºì°ü£¬Ð»Ð»»ØÌû
΢³¾¡¢ÃÎÏë(½ð±Ò+5): лл²ÎÓ룡 2011-05-29 16:31:45
±¾Ìû½öÂ¥Ö÷¿É¼û
3Â¥2011-05-28 08:55:10
ÒÑÔÄ   ÉêÇë³ÌÐòÇ¿Ìû   »Ø¸´´ËÂ¥   ±à¼­   ²é¿´ÎÒµÄÖ÷Ò³

holmescn

½ð³æ (ÕýʽдÊÖ)

¡ï ¡ï ¡ï ¡ï ¡ï ¡ï
Сľ³æ(½ð±Ò+0.5):¸ø¸öºì°ü£¬Ð»Ð»»ØÌû
΢³¾¡¢ÃÎÏë(½ð±Ò+5): лл²ÎÓ룡 2011-05-29 16:31:59
Íê³ÉÁËPython°æ
CODE:
#/bin/env python

ones = ["", "one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine", "ten",
        "eleven", "twelve", "thirteen", "fourteen",
        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]

tens = ["", "twenty", "thirty", "forty", "fifty",
        "sixty", "seventy", "eighty", "ninety"]

words = ["", " thousand", " million", " billion"]

def num2words(n):
    "Convert an integer to english words"
    def part(num, s):
        "Convert a part of numbers"
        if num < 20:
            return s + ones[num]

        if num < 100:
            return s + tens[num / 10 - 1] + " " +  ones[num % 10]

        return ones[num / 100] + " hundred " + s + part(num % 100, "")

    # divided into several part
    # each part contain three numbers
    t = 1000
    # results list
    r = []
    i = 0
    while n > 0:
        # add "and" into the word
        if i == 0 and n > 100 and n % 100 != 0:
            r.append(part(n % t, 'and ') + words[i])
        else:
            r.append(part(n % t, "") + words[i])
        n /= t
        i += 1
    r.reverse()
    return r


if __name__ == "__main__":
    s = 0
    for i in range(1, 1001):
        for w in num2words(i):
            s += sum([len(x) for x in w.split()])
    print "total length:", s

ͬÑùÍÆ¼öÂ¥ÉÏÓÃBBCode·¢²¼´úÂë.
4Â¥2011-05-28 13:57:38
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[»ù½ðÉêÇë] Ã÷ÌìÓ¦¸Ã¿É²éÁË£¡£¿ +5 chengyan1220 2026-08-23 5/250 2026-08-24 23:28 by ÎÒ4´ó°×²Ë
[»ù½ðÉêÇë] Èç¹û´Ë¿ÌÄãÕýÔÚΪ¹ú»ù¸Ðµ½½¹ÂÇ£¬²»·ÁÀ´ÌýÌýÕâÊס¶»ù½ðÖ®Íâ¡· +7 scalable 2026-08-24 7/350 2026-08-24 23:01 by anata1209
[»ù½ðÉêÇë] ûÓÐÈκÎÏûÏ¢-ÊDz»ÊǾÍÁ¹ÁË +7 ͼÀ²Í¼À² 2026-08-24 8/400 2026-08-24 22:00 by maomao_da
[ÂÛÎÄͶ¸å] ÊÛSCI-T0PÎÄÕ£¬ÎÒ:8O.5.5.1.O.54,¿ÆÄ¿ÆëÈ«,¿É+¼± +3 0XLacIJUOj8D 2026-08-24 3/150 2026-08-24 21:59 by BZKMTicpDhFj
[»ù½ðÉêÇë] ÄÜ·ñÍ˳ö²ÎÓëµÄÃæÉÏÏîÄ¿½â³ýÏÞÏî +21 koalala 2026-08-24 24/1200 2026-08-24 19:25 by ¼ÒÓëÔ¶·½
[»ù½ðÉêÇë] 2026¹ú×ÔÈ»º¯ÆÀ·Ñµ½ÕË +17 ÑòÑü°å 2026-08-21 19/950 2026-08-24 16:52 by iaeyuan
[»ù½ðÉêÇë] ½¨Òé»ù½ð·¢²¼Ìáǰ¸ø³öÃ÷È·µÄʱ¼äµã +13 kulium 2026-08-21 16/800 2026-08-24 16:27 by superceng
[»ù½ðÉêÇë] ¹À¼ÆÊÇÖÜËÄ +4 archvillain 2026-08-18 4/200 2026-08-24 13:53 by zzuzxg
[»ù½ðÉêÇë] ·¶½øÖоÙÒ»ÎĵÄÖÐÐÄ˼Ïë +6 Ñ׻ƹóëÐ 2026-08-22 7/350 2026-08-24 11:58 by 6543yes
[»ù½ðÉêÇë] ÈÃÎÒÖÐÒ»¸öÃæÉϰɣ¡ +13 ´óƼ1987 2026-08-20 16/800 2026-08-24 10:23 by ̫ɵÁË
[»ù½ðÉêÇë] ¿ÆÑй¶ùÌ«ÄÑÁË +18 ÎÒ4´ó°×²Ë 2026-08-20 19/950 2026-08-24 09:47 by ¿­¶÷¹ãÊ¢´ß»¯·ÖÎ
[»ù½ðÉêÇë] 2026ÄêµÄ¹ú¼ÒÉç¿Æ»ù½ðÏîĿͨѶÆÀÉóµÄйæÔòÓëж¯Ïò¡¢ÐÂÌôÕ½ +4 process2012 2026-08-23 5/250 2026-08-23 19:58 by jurkat.1640
[½Ìʦ֮¼Ò] Ìø²ÛºóÔÚÑÐÏîÄ¿Ôõô°ì£¿ +5 ¼òµ¥»¯xn 2026-08-22 10/500 2026-08-23 12:38 by ¼òµ¥»¯xn
[»ù½ðÉêÇë] ½ñÌì·Å°ñÂ𣿠+15 ²¼²¼ºÍÒ»¶þ 2026-08-19 16/800 2026-08-23 09:55 by ÕÅ´ºÉú
[»ù½ðÉêÇë] Ö»ÓÐÿÄêÕâÖÖʱºòÀ´¹ä¹äСľ³æ +24 yaoyewhu2008 2026-08-20 26/1300 2026-08-22 17:43 by kammury
[»ù½ðÉêÇë] ¿´À´½ñÌì²»»á·Å°ñÁË£¿ +8 chengyan1220 2026-08-21 11/550 2026-08-21 17:52 by dcqxinyang
[»ù½ðÉêÇë] ʱ¼ä´ÁÓÖ±äÁË +13 wuchongjun 2026-08-20 19/950 2026-08-21 17:21 by ×Ïɼ´¼
[»ù½ðÉêÇë] Ó¦¸ÃÊÇÏÂÖÜÈý26ÈÕ¹«²¼Á˰ɣ¿ +4 ¹þ¹þ¸ò£¿ 2026-08-21 4/200 2026-08-21 10:58 by Vivilian
[»ù½ðÉêÇë] ÖØÒªÏûÏ¢£¬ÖÐÎçϵͳÔÚά»¤ +11 yuleib84 2026-08-18 12/600 2026-08-20 11:09 by xskun
[»ù½ðÉêÇë] Ã÷Ìì·Å°ñ£¿ +5 Shxjjxjkx 2026-08-18 5/250 2026-08-18 18:14 by -´ó´ó´ó´ó´ó-
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û