아래의 중첩 루프를 단순화하는 방법이 있는지 궁금합니다. 문제는 각 루프의 반복자가 이전 루프의 내용에 의존한다는 것입니다.Python : 중첩 된 FOR 루프를 단순화 하시겠습니까?
# Find the number of combinations summing to 200 using the given list of coin
coin=[200,100,50,20,10,5,2,1]
total=[200,0,0,0,0,0,0,0]
# total[j] is the remaining sum after using the first (j-1) types of coin
# as specified by i below
count=0
# count the number of combinations
for i in range(int(total[0]/coin[0])+1):
total[1]=total[0]-i*coin[0]
for i in range(int(total[1]/coin[1])+1):
total[2]=total[1]-i*coin[1]
for i in range(int(total[2]/coin[2])+1):
total[3]=total[2]-i*coin[2]
for i in range(int(total[3]/coin[3])+1):
total[4]=total[3]-i*coin[3]
for i in range(int(total[4]/coin[4])+1):
total[5]=total[4]-i*coin[4]
for i in range(int(total[5]/coin[5])+1):
total[6]=total[5]-i*coin[5]
for i in range(int(total[6]/coin[6])+1):
total[7]=total[6]-i*coin[6]
count+=1
print count
도 참조 [ "반복적으로 지정된 양을 생산하고 모든 동전의 조합을 찾아"] (http://stackoverflow.com/q/9815077/90527), [ "숙제 질문 및 답변 방법]"(http://meta.stackexchange.com/q/10811/). – outis
당신의 문제는 네 스티드 루프를 간소화하는 것이 아니라 똑똑한 알고리즘을 가져야합니다. – xvatar
@outis - 숙제 일지라도 약간 짜증이납니다.하지만 여기에는 약간의 노력이 있습니다. 제약 라이브러리가 조금 있을지도 모릅니다. 어쨌든 까다 롭습니다 ... –