https://www.acmicpc.net/problem/1918
후위표기식으로 만들자.
# -*- encoding: cp949 -*- s=raw_input() stack = [] answer = '' for c in s: if c.isalpha(): answer += c else: if len(stack)==0: stack.append(c) else: while stack: if stack[-1] in ['*','/'] and c not in ['(',')']: answer += stack.pop() elif stack[-1] in ['+','-'] and c in ['+','-']: answer += stack.pop() else: break if c!=')': stack.append(c) if c==')': while True: p = stack.pop() if p=='(': break answer += p while stack: answer += stack.pop() print answer
https://www.acmicpc.net/problem/1935
깔끔하게 짜지못함. 뭔가 맘에안드는 코드.....
# -*- encoding: cp949 -*- n=input() s=raw_input() d = dict([[chr(i+65),raw_input()+'.0'] for i in xrange(n)]) stack = [] cnt = 0 while not s.isdigit(): flag = True for i in xrange(len(s)): if s[i] in ['+','-','*','/']: answer = str(eval(d[s[i-2]]+s[i]+d[s[i-1]])) d[chr(97+cnt)] = answer s = s[:i-2] + chr(97+cnt) + s[i+1:] cnt += 1 flag = False break if flag: break print '{:.2f}'.format(float(d[s]))
'algorithm > problem solving' 카테고리의 다른 글
백준 랭킹 (0) | 2017.04.16 |
---|---|
acmicpc.net 13565(BFS,DFS), 1261(다익스트라) (0) | 2016.11.27 |
acmicpc.net 2805(이분 탐색), 9184(DP 메모이제이션 기초), 2688(DP) (0) | 2016.11.19 |
acmicpc.net 1254(DP, 팰린드롬), 1695,5502(DP, 팰린드롬), 11203(이진 트리) (0) | 2016.11.12 |
acmicpc.net 2011(DP), 2240(DP), 2666(DP), 2624(DP) (0) | 2016.11.11 |