문제 : 1부터 5까지의 숫자를 영어로 쓰면 one, two, three, four, five 이고,
각 단어의 길이를 더하면 3 + 3 + 5 + 4 + 4 = 19 이므로 사용된 글자는 모두 19개입니다.
1부터 1,000까지 영어로 썼을 때는 모두 몇 개의 글자를 사용해야 할까요?
참고: 빈 칸이나 하이픈('-')은 셈에서 제외하며, 단어 사이의 and 는 셈에 넣습니다.
예를 들어 342를 영어로 쓰면 three hundred and forty-two 가 되어서 23 글자,
115 = one hundred and fifteen 의 경우에는 20 글자가 됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | n = [] n20_100 = [] n.append("one") n.append("two") n.append("three") n.append("four") n.append("five") n.append("six") n.append("seven") n.append("eight") n.append("nine") n.append("ten") n.append("eleven") n.append("twelve") n.append("thirteen") n.append("fourteen") n.append("fifteen") n.append("sixteen") n.append("seventeen") n.append("eighteen") n.append("nineteen") print n n20_100.append("twenty") n20_100.append("thirty") n20_100.append("forty") n20_100.append("fifty") n20_100.append("sixty") n20_100.append("seventy") n20_100.append("eighty") n20_100.append("ninety") n20_100.append("hundred") print n20_100 result = 0 for cnt in range(1, 1001): tmp = "" print cnt if cnt < 20: tmp = n[cnt-1] elif cnt >=20 and cnt < 100: tmp = n20_100[cnt/10-2] if cnt %10 >0 : tmp += n[cnt%10-1] elif cnt >=100 and cnt < 1000: tmp = n[cnt/100-1] tmp += "hundred" tenCnt = cnt % 100 if tenCnt == 0: print "%d start !!!!" %cnt elif tenCnt < 20: tmp += "and" tmp += n[tenCnt -1] elif tenCnt >=20 and tenCnt < 100: tmp += "and" tmp += n20_100[tenCnt/10-2] if tenCnt%10 >0: tmp += n[tenCnt%10-1] elif cnt >=1000: tmp = n[cnt/1000-1] tmp += "thousand" print tmp result += len(tmp) print "result is %d" % result | cs |
ninehundredandninetysix
997
ninehundredandninetyseven
998
ninehundredandninetyeight
999
ninehundredandninetynine
1000
onethousand
result is 21124
'* Computer Science > Project Euler' 카테고리의 다른 글
project euler 19. problem (0) | 2016.07.29 |
---|---|
project euler 18. problem (0) | 2016.07.28 |
project euler 16. problem (0) | 2016.07.26 |
project euler 15. problem (0) | 2016.07.25 |
project euler 14. problem (0) | 2016.07.24 |