[Project Euler] Problem 17

원문: Problem 17
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까지 모든 숫자를 단어로 쓴다면 얼마나 많은 문자들이 쓰였는가?

참고: 공백과 하이픈은 세지 마시오. 예를 들어, 342 (three hundred and forty-two) 는 23개 문자를 포함하고 115 (one hundred and fifteen) 은 20개 문자를 포함한다. 숫자를 쓸 때 and 의 사용은 영국식 표현의 준수이다.

Python
>>> num_dict = {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}
>>> for n in range(1, 100) :
    if n>10 and n<20 :
        total += num_dict[n]
    else :
        total += num_dict[n//10 * 10]
        total += num_dict[n%10]
else :
    total *= 10
    for n in range(1,10) :
        total += (num_dict[n] + 7) * 100 + 3 * 99
    total += 11
    print(total)
접기

댓글 없음:

댓글 쓰기

크리에이티브 커먼즈 라이선스