-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14888.py
77 lines (60 loc) · 1.62 KB
/
14888.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sys
from collections import deque
N=int(sys.stdin.readline())
operand=list(map(int,sys.stdin.readline().split()))#피연산자
operator=list(map(int,sys.stdin.readline().split()))#연산자
op=[]
operand=deque(operand)
for i in range(len(operator)):
for j in range(operator[i]):
if i==0:
op.append('+')
elif i==1:
op.append('-')
elif i==2:
op.append('*')
else:
op.append('/')
real=[]
def backtracking(sol,op,operand):
if len(sol)==len(op):
answer=[operand[0]]
i=0
j=1
while i!=len(op):
x=answer.pop()
y=operand[j]
z=sol[i]
if z=='+':
ans=x+y
answer.append(ans)
elif z=='-':
ans=x-y
answer.append(ans)
elif z=='*':
ans=x*y
answer.append(ans)
else:
if x<0:
ans=-((-x)//y)
else:
ans=x//y
answer.append(ans)
i+=1
j+=1
global real
real.append(answer[0])
return
candidate=[]
for i in op:
candidate.append(i)
for i in range(len(sol)):
if sol[i] in candidate:
candidate.remove(sol[i])
for i in candidate:
sol.append(i)
backtracking(sol,op,operand)
sol.pop()
backtracking([],op,operand)
print(max(real))
print(min(real))