From 09883109b9d06bd467967a36292660090a988f32 Mon Sep 17 00:00:00 2001 From: pu2rile <3o920@naver.com> Date: Thu, 11 Jul 2024 22:13:22 +0900 Subject: [PATCH] 2024-07-11 --- pu2rile/README.md | 4 +++- ...24\355\225\231\354\213\235\353\237\211.py" | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 "pu2rile/\354\212\244\355\203\235/\355\231\224\355\225\231\354\213\235\353\237\211.py" diff --git a/pu2rile/README.md b/pu2rile/README.md index 70f15e8..2376f74 100644 --- a/pu2rile/README.md +++ b/pu2rile/README.md @@ -9,4 +9,6 @@ | 5차시 | 2024.04.10 | 그리디 알고리즘 | [국회의원 선거](https://www.acmicpc.net/problem/1417) | [#18](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/18#issue-2235862658) | 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) \ No newline at end of file +| 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) +| 10차시 | 2024.07.11 | 스택 | [화학식량](https://www.acmicpc.net/problem/2257) | [#34]() \ No newline at end of file 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