Skip to content

Conversation

@ehdtndla123
Copy link

문제 해결 아이디어

브루드 포스를 이용하여 모든 연산을 백트래킹 하며 계산하고 answer 리스트에 값을 넣고 max,min을 출력한다.

screenshot

image

핵심 코드

#백트래킹 사용하여 모든 수식 풀기
#numList= 숫자 리스트 operList= 연산자 리스트 count = 연산 횟수 k= count 만큼 연산 했을때의 수
def solve(numList,operList,k,count):
    if count!=n-1:
        num=numList[count+1]
        for i in range(4):
            if operList[i]!=0:
                operList[i]-=1
                count+=1
                if i==0:
                    solve(numList,operList,k+num,count)
                elif i==1:
                    solve(numList,operList,k-num,count)
                elif i==2:
                   solve(numList,operList,k*num,count)
                elif i==3:
                    temp=0
                    # 음수일경우 양수로 변환후 몫 구하고 음수로 바꾸고 넣기
                    if k<0:
                        temp=-k
                        temp=temp//num
                        temp=-temp
                    else:
                        temp=k//num
                    solve(numList,operList,temp,count)
                #백트래킹
                count-=1
                operList[i]+=1
                
    # 연산을 모두 했을 경우
    elif count==(n-1):
        answer.append(k)
        return 

@anjm1020
Copy link

elif count==(n-1):
        answer.append(k)
        return 

이 부분이 백트래킹에 대해 중단점을 걸어주는 부분 같은데,
함수 상단으로 올리면 가시성에 더 좋을 것 같습니다

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants