π μλ‘
μ¬μ€ μ λ²μ£Όμ νμνκΈ°μ κ΄λ ¨ λ¬Έμ λ₯Ό νμμλ€. μκΈ°!
κ°λ μ μ΄ν΄νμΌλ λ λ‘λ¨Ήμ μκ°μΌλ‘ 골λ 3μ λμ νλ€κ° 보기μ’κ² μ€ν¨νλ€γ .γ
μμ λ¬Έμ νκ³ μ΄λ² λ¬Έμ μ μ°¨μ΄μ μ μμμ΄λ€. νμ νκΈ°μ2λ² λ¬Έμ λ νμνκΈ°μμ μ€μνκΈ°μμΌλ‘ λ°κΎΈμ΄ κ°μ κ³μ°νλ κ±°μλ€λ©΄, μ΄λ² λ¬Έμ λ μ€μνκΈ°μμ νμνκΈ°μμΌλ‘ λ°κΎΈκΈ°λ§ νλ©΄ λλ κ²μ΄μλ€!
μ€μνκΈ°μμ νμνκΈ°μμΌλ‘ λ³κ²½
νμνκΈ°μμ μ€μνκΈ°μμΌλ‘ λ°κΎΈκ³ κ°μ μΆλ ₯
μ΄λ κ² λ΄€μ λ νμκ° λ μ΄λ €μΈ κ² κ°μ§λ§,, μ€μνκΈ°μμ νμνκΈ°μμΌλ‘ λ°κΎΈλ €λ©΄ κ΄νΈμΉλ μμ μ ν΄μΌνλ€.
μ΄κ² λκ΄μ΄μλ€...
π λ¬Έμ νμ΄
1. κ³±νκΈ°μ λλκΈ°κ° μ°μ μμκ° λμΌλ κ΄νΈλ₯Ό μμ΄μ€λ€.
2. λνκΈ°μ λΊ΄κΈ°μ κ΄νΈλ₯Ό μμ΄μ€λ€.
3. μ€μ νκΈ°μμ νμ νκΈ°μμΌλ‘ λ°κΏμ€λ€. μμΈν λ΄μ©μ μμ κΈ μ°Έμ‘°..
π» μ€ν¨μ½λ!
sick = list(input())
#print(sick)
stk = []
first = {"*", "/"}
second = {"+","-"}
gualho = []
backtrack = [] # λ€λ‘ κ°λ κ΄νΈ 체ν¬
some = []
result = []
check = False
check2 = False
c1 = sick.count("+") + sick.count("-") + sick.count("*") + sick.count("/")
c2 = sick.count("(")
if c1 != c2:
for i in sick:
if i == "(":
gualho.append(i)
elif i == ")":
gualho.pop()
if i in first:
a = stk.pop()
if a == ")":
backtrack.append(a)
some.append(i)
some.append(a)
while backtrack:
b = stk.pop()
some.append(b)
if b == ")":
backtrack.append(b)
elif b == "(":
backtrack.pop()
some.append("(")
some = some[::-1]
stk.extend(some)
some = []
check2 = True
else:
stk.append("(")
stk.append(a)
stk.append(i)
check = True
elif check2 == True and i == ")" and len(gualho) == 0:
stk.append(i)
stk.append(")")
check2 = False
elif check2 == True and 65 <= ord(i) <= 90 and len(gualho) == 0:
stk.append(i)
stk.append(")")
check2 = False
elif i not in first and check == True and len(gualho) == 0:
stk.append(i)
stk.append(")")
check = False
elif i not in first and check == True and len(gualho) != 0:
stk.append(i)
elif i not in first and check == False:
stk.append(i)
result = stk.copy()
else:
result = sick
gualo_cnt = stk.count("(")
yensan_cnt = stk.count("+") + stk.count("-") + stk.count("*") + stk.count("/")
sick = stk.copy()
stk = []
if gualo_cnt != yensan_cnt:
for i in sick:
if i == "(":
gualho.append(i)
elif i == ")":
gualho.pop()
if i in second:
a = stk.pop()
if a == ")":
backtrack.append(a)
some.append(i)
some.append(a)
while backtrack:
b = stk.pop()
some.append(b)
if b == ")":
backtrack.append(b)
elif b == "(":
backtrack.pop()
some.append("(")
some = some[::-1]
stk.extend(some)
some = []
check2 = True
else:
stk.append("(")
stk.append(a)
stk.append(i)
check = True
elif i in first:
stk.append(i)
elif check2 == True and i == ")" and len(gualho) == 0:
stk.append(i)
stk.append(")")
check2 = False
elif check2 == True and 65 <= ord(i) <= 90 and len(gualho) == 0:
stk.append(i)
stk.append(")")
check2 = False
elif i not in first and check == True and len(gualho) == 0:
stk.append(i)
stk.append(")")
check = False
elif i not in first and check == True and len(gualho) != 0:
stk.append(i)
elif i not in first and check == False:
stk.append(i)
result = stk
else:
pass
print(*result, sep="")
# μ΄μ νμνκΈ°μμΌλ‘ λ³ν
tmp = []
for k in result:
try:
if k == "(":
pass
elif 65 <= ord(k) <= 90:
print(k, end="")
elif k == ")":
print(tmp.pop(), end="")
else:
tmp.append(k)
except IndexError:
pass
if len(tmp) != 0:
print(*tmp)
μ½λμ λ¬Έμ μ
μ€λ³΅κ΄νΈ input : (A+B)*C, output : (((A+B))*C)
κ΄νΈκ° νλλ§ μμ΄λ λλλ° κ΄νΈκ° μλμ±λ‘ μ λ ₯ν΄μ¨λ€λ©΄ κ΄νΈκ° μ€λ³΅ν΄μ λ€μ΄κ°λ€...
κ΄νΈκ° λ€ μλΊν input : A+B*C*((D-E)*G) => (A+(((B*C)*(((D-E)*G))
보μλ©΄ μΌκ΄νΈλ μ°κ΄νΈ κ°―μκ° λ§μ§ μλ€...
μ΄λ κ² κ΄νΈ μμ°λλ°λΆν° λ¬Έμ κ° μκ²Όκ³ , μ½λκ° λ무 κΈΈμ΄μ Έ λλ λ΄ μ½λκ° λμ§ λͺ¨λ₯΄κ² λμλ€!
π λ¬Έμ λ§ν¬ : νμ νκΈ°μ
'Problem Solving > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€] 25192λ²: μΈμ¬μ± λ°μ κ³°κ³°μ΄ - νμ΄μ¬ (0) | 2022.05.22 |
---|---|
[λ°±μ€] 2606λ²: λ°μ΄λ¬μ€ - νμ΄μ¬ (0) | 2022.05.19 |
[λ°±μ€] 1629λ²: κ³±μ - νμ΄μ¬ (1) | 2022.05.16 |
[λ°±μ€] 14425λ²: λ¬Έμμ΄ μ§ν© - νμ΄μ¬ (0) | 2022.05.15 |
[λ°±μ€] 7795λ²: λ¨Ήμ κ²μΈκ° λ¨Ήν κ²μΈκ° - νμ΄μ¬ (0) | 2022.05.09 |