2016-08-12 3 views
-1

질문 하나, 둘, 셋, 넷, 다섯, 다음 + 3가 3 + 5 + 4 + 4 = 총 19 자.프로젝트 오일러 # 17 파이썬 "번호 문자 카운트"여기

1에서 1000 (1000)까지의 모든 숫자가 단어로 쓰여진 경우 몇 개의 문자가 사용됩니까?

참고 : 공백이나 하이픈을 넣지 마십시오. 예를 들어, 342 (삼백 사십)는 23 자, 115 자 (115)는 20자를 포함합니다. 숫자를 쓸 때 "및"을 사용하면 영국의 사용법을 준수합니다.

내 코드가 정답을 출력하지 않는 이유를 모르겠습니다. 나는 여러 번 검사했고 내가 놓친 것을 찾을 수 없다. 여기 내 코드 :

to_19 = [0,3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8] 
tens = [0,3,6,6,5,5,5,7,6,6] 
hundred = 7 
thousand = 8 
total = 0 
for i in range(1,1000): 
    u = i%10 
    t = int(((i%100)-u) /10) 
    h = int(((i%1000)-(t*10)-u) /100) 
    print(h,t,u) 

    if i < 20:               #the number is less than 20 
     total += to_19[i] 
    elif h != 0 and (t != 0 or u != 0):         #the number is over 100 but not a multiple of 100 
     if t == 0 or t == 1:           #the number is between x01 and x19 
      total += to_19[h] + hundred + 3 + to_19[(t * 10) + u] 
     else:               #the number is between x20 and x99 
      total += to_19[h] + hundred + 3 + tens[t] + to_19[u] 
    elif t == 0 and u == 0:            #the number is a multiple of 100 
     total += to_19[h] + hundred 
    else:                #the number is between 20 and 99 
     total += tens[t] + to_19[u] 

print(total+thousand) 
#21121 is wrong 

미리 감사드립니다!

+0

범위 (1,1000)은 1에서 999까지를 포함하지 않고 1000을 포함합니다. – mkaran

+0

그래,하지만 마지막 줄의 최종 결과에 1000을 추가했습니다. –

답변

2

코드가 충분히 합리적인 것처럼 보입니다. 나는 u, th의 정의에 많은 생각을 넣지는 않았지만 올바른 것으로 보입니다. 내가 너를 잃어버린 유일한 일은 "천"의 "하나"이다.

+0

그리고 1000에 대해 "백"을 추가합니다. –

+0

오 하나님. Yeaahhh. * facepalm *. Brandon에게 감사드립니다. –