-
[14888] 연산자 끼워넣기 (Python)[Python] 알고리즘/Silver 2022. 5. 24. 21:30
[문제]
https://www.acmicpc.net/problem/14888
알고리즘 분류는 브루트포스 알고리즘, 백트래킹 입니다.
저는 Python의 permutations(조합)을 사용하여 풀었습니다.
연산자 우선순위를 무시하고 앞에서 부터 계산해야 하고, 음수를 양수로 나눌 때의 조건도 지정해주어야 합니다.
[코드]
import sys from itertools import permutations N = int(sys.stdin.readline()) A = list(map(int, sys.stdin.readline().split())) plus, minus, mul, div = map(int, sys.stdin.readline().split()) st = "+ " * plus + "- " * minus + "* " * mul + "// " * div li = st.split() test = set(permutations(li, len(li))) res = list() for i in test: sum = A[0] for j in range(len(i)): if i[j] == "+": sum += A[j + 1] elif i[j] == "-": sum -= A[j + 1] elif i[j] == "*": sum *= A[j + 1] elif i[j] == "//": if sum < 0: sum = -(abs(sum) // A[j + 1]) else: sum //= A[j + 1] res.append(sum) print(max(res), min(res))
'[Python] 알고리즘 > Silver' 카테고리의 다른 글
[11053] 가장 긴 증가하는 부분 수열 (Python) (0) 2022.05.06 [1912] 연속합 (Python) (0) 2022.04.30 [9184] 신나는 함수 실행 (Python) (1) 2022.04.12 [10819] 차이를 최대로 (Python) (0) 2022.04.12 [1182] 부분수열의 합 (Python) (0) 2022.04.04