24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 1423  |  回复: 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的回帖

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效率还是不高
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的回帖

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的回帖
相关版块跳转 我要订阅楼主 libralibra 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 生物与医药求调剂 +5 heguanhua 2026-04-05 6/300 2026-04-05 22:58 by Hdyxbekcb
[考研] 086000生物与医药求调剂 +3 老天眷顾之人 2026-03-31 3/150 2026-04-05 22:24 by syh9288
[考研] 考研生物学考A区211,初试322,科目生化和生物综合,求调剂 +6 。。。54 2026-04-03 6/300 2026-04-05 14:54 by JOKER0401
[考研] 一志愿江南大学085501机械工程专硕326分,本科佳木斯大学 +5 顾若浮生 2026-04-03 9/450 2026-04-05 09:57 by 1753564080
[考研] 一志愿北京化工大学,初试成绩350求调剂 +9 沿岸?贝壳 2026-04-04 14/700 2026-04-05 01:09 by 沿岸?贝壳
[考研] 333求调剂 +12 wfh030413@ 2026-04-03 13/650 2026-04-04 21:02 by jj987
[考研] 081200-11408-276学硕求调剂 +5 崔wj 2026-03-31 5/250 2026-04-04 19:45 by 1753564080
[考研] 291求调剂 +4 迷蒙木木 2026-04-01 5/250 2026-04-04 15:59 by sihailian3
[考研] 考研调剂 +4 zybz冲冲冲 2026-04-03 6/300 2026-04-04 13:08 by zybz冲冲冲
[考研] 一志愿双非085502,267分,过四级求调剂 +3 再忙也要吃饭啊 2026-04-03 3/150 2026-04-04 05:03 by gswylq
[考研] 321求调剂 +17 y-yh 2026-04-01 20/1000 2026-04-03 12:57 by y-yh
[考研] 282求调剂 +5 呼吸都是减肥 2026-03-31 5/250 2026-04-03 12:03 by 1753564080
[考研] 建环,能源,土木老师路过看一看!!! +5 嘿嘿uu 2026-04-01 5/250 2026-04-03 11:47 by znian
[考研] 274求调剂 +10 薛定谔的虎。 2026-04-01 10/500 2026-04-03 10:13 by tianyyysss
[考研] 085602化工求调剂(331分) +9 111@127 2026-03-30 9/450 2026-04-02 20:00 by dick_runner
[考研] 学硕化学工程与技术,一志愿中国海洋大学320+求调剂 +8 披星河 2026-04-02 8/400 2026-04-02 14:12 by oooqiao
[考研] 085900土木水利336分求调剂 +4 Zhangjiangj 2026-03-31 6/300 2026-04-02 11:40 by 1753564080
[考研] 一志愿安徽大学计算机科学与技术学硕,331分求调剂 +5 蒋昌鹏qtj 2026-04-01 5/250 2026-04-02 08:10 by fxue1114
[考研] 08工科,295,接受跨专业调剂 +6 lmnlzy 2026-03-31 6/300 2026-04-01 11:02 by 逆水乘风
[考研] 322求调剂 +8 三水sss 2026-04-01 8/400 2026-04-01 10:19 by 唐沐儿
信息提示
请填处理意见