728x90
오늘은 코테 스터디 2일차. 이 문제는 풀 때 생각을 정리하면서 풀었다. 문제를 정리한 연습장 내용도 올려보는데, 다른분들께 도움이 될지는 모르겠다. ps. 스터디를 하고 벌금을 걸어두니까 확실히 잘 하게 된다. 매일매일 2문제씩 꼬박꼬박 풀기🔥🔥
연산자 끼워넣기
- 문제링크: https://www.acmicpc.net/problem/14888
- 난이도: 실버1
- 분류: bruteforce, backtracking
- 직접 푼 여부: O
- 언어: 파이썬
생각과정
풀이
import sys
n= int(sys.stdin.readline())
numlist = list(map(int, sys.stdin.readline().split()))
opernum = list(map(int, sys.stdin.readline().split()))
maxanswer = -10**9 # 최댓값/ 최솟값이 10억이라고 되어있으므로
minanswer = 10**9
def dfs(op, cnt, tempans):
global opernum, numlist, maxanswer, minanswer
# 작업
if op ==0:
tempans+=numlist[cnt]
elif op==1:
tempans-=numlist[cnt]
elif op==2:
tempans*=numlist[cnt]
elif op ==3:
tempans=int(tempans/numlist[cnt])
# exit
if cnt ==n-1: # operator 다 사용하면
maxanswer = max(maxanswer, tempans)
minanswer = min(minanswer, tempans)
return
# 재귀
for i in range(4):
if opernum[i] >0: # 사용할 수 있는 연산자 있으면
opernum[i]-=1 #하나 사용했으니
dfs(i,cnt+1, tempans)
opernum[i]+=1 # 나올때는 다시 +1
#초기설정
tempans=numlist[0]
cnt=0 #사용한 연산주 개수
dfs(-1, cnt, tempans)
print(int(maxanswer))
print(int(minanswer))
728x90
'취준! ✒ > 삼성' 카테고리의 다른 글
[코테준비] 6. 백준- 경사로(14890) python "삼성 SW 역량 테스트 기출 문제" (0) | 2023.09.17 |
---|---|
[코테준비] 5. 백준- 주사위 굴리기(14499) python "삼성 SW 역량 테스트 기출 문제" (0) | 2023.09.16 |
[코테준비] 4. 백준- 시험 감독(13458) python "삼성 SW 역량 테스트 기출 문제" (0) | 2023.09.13 |
[코테준비]2. 백준- 스타트와 링크(14889) python "삼성 SW 역량 테스트 기출 문제" (0) | 2023.09.11 |
[코테준비] 1. 백준- 퇴사(14501) python "삼성 SW 역량 테스트 기출 문제" (1) | 2023.09.10 |