diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..097f6a0 Binary files /dev/null and b/.DS_Store differ diff --git a/pu2rile/.DS_Store b/pu2rile/.DS_Store new file mode 100644 index 0000000..b798810 Binary files /dev/null and b/pu2rile/.DS_Store differ diff --git a/pu2rile/README.md b/pu2rile/README.md index b3f5ec3..7d06827 100644 --- a/pu2rile/README.md +++ b/pu2rile/README.md @@ -10,4 +10,5 @@ | 6차시 | 2024.05.10 | 그리디 알고리즘 | [ATM](https://www.acmicpc.net/problem/11399) | [#25](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/25#issue-2289086909) | 7차시 | 2024.05.10 | 완전 탐색 알고리즘 | [영화감독 숌](https://www.acmicpc.net/problem/1436) | [#26](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/25#issue-2289086909) | 8차시 | 2024.05.14 | 그리디 알고리즘 | [팔](https://www.acmicpc.net/problem/1105) | [#28](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/28#issue-2295901384) -| 9차시 | 2024.05.27 | 구현 | [오늘도 졌다](https://www.acmicpc.net/problem/14582) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/29#issue-2320060288) \ No newline at end of file +| 9차시 | 2024.05.27 | 구현 | [오늘도 졌다](https://www.acmicpc.net/problem/14582) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/29#issue-2320060288) +| 10차시 | 2024.07.11 | 스택 | [화학식량](https://www.acmicpc.net/problem/2257) | [#35](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/35#issue-2403173169) \ No newline at end of file diff --git "a/pu2rile/\354\212\244\355\203\235/.DS_Store" "b/pu2rile/\354\212\244\355\203\235/.DS_Store" new file mode 100644 index 0000000..efd16c2 Binary files /dev/null and "b/pu2rile/\354\212\244\355\203\235/.DS_Store" differ diff --git "a/pu2rile/\354\212\244\355\203\235/\355\231\224\355\225\231\354\213\235\353\237\211.py" "b/pu2rile/\354\212\244\355\203\235/\355\231\224\355\225\231\354\213\235\353\237\211.py" new file mode 100644 index 0000000..e964f10 --- /dev/null +++ "b/pu2rile/\354\212\244\355\203\235/\355\231\224\355\225\231\354\213\235\353\237\211.py" @@ -0,0 +1,22 @@ +chemical = input() +stack = [] +atomic = {'H':1, 'C':12, 'O':16} # 원자량 딕셔너리 + +for c in chemical: + if c =='(': + stack.append(c) + elif c == 'H' or c == 'C' or c == 'O': + stack.append(atomic[c]) # 입력받은 원자의 원자량을 스택에 추가 + elif c == ')': + temp = 0 # 닫는 괄호를 만나면 temp 초기화 + while True: + if stack[-1] == '(': # 스택의 top이 여는 괄호면 + stack.pop() # 스택에서 여는 괄호를 삭제 + stack.append(temp) # 스택에 temp 추가 + break + else: + temp += stack.pop() # 여는 괄호 전까지의 스택 안의 모든 값을 temp에 저장 + else: # c가 숫자라면 + stack.append(stack.pop()*int(c)) # 스택의 top 값과 숫자를 곱하여 스택에 추가 + +print(sum(stack)) \ No newline at end of file