ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

1935๋ฒˆ: ํ›„์œ„ ํ‘œ๊ธฐ์‹2

์ฒซ์งธ ์ค„์— ํ”ผ์—ฐ์‚ฐ์ž์˜ ๊ฐœ์ˆ˜(1 ≤ N ≤ 26) ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ํ›„์œ„ ํ‘œ๊ธฐ์‹์ด ์ฃผ์–ด์ง„๋‹ค. (์—ฌ๊ธฐ์„œ ํ”ผ์—ฐ์‚ฐ์ž๋Š” A~Z์˜ ์˜๋Œ€๋ฌธ์ž์ด๋ฉฐ, A๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ N๊ฐœ์˜ ์˜๋Œ€๋ฌธ์ž๋งŒ์ด ์‚ฌ์šฉ๋˜๋ฉฐ, ๊ธธ์ด๏ฟฝ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ํ›„์œ„ ํ‘œ๊ธฐ์‹์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์€ ํ›„์œ„ ํ‘œ๊ธฐ์‹์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ๊ฐ„๋‹จํ•˜๋‹ค. ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ stack์— push ํ•˜๊ณ , ์—ฐ์‚ฐ์ž๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด stack์˜ ๊ฐ’ ์ค‘ 2๊ฐœ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ์—ฐ์‚ฐ์„ ํ•˜๋ฉด ๋œ๋‹ค. ํ•ด๋‹น ๋ฌธ์ œ์—์„œ๋Š” A - Z์™€ ๊ฐ™์ด ์˜์–ด ๋Œ€๋ฌธ์ž๊ฐ€ ์ฃผ์–ด์ง€๊ณ , ์ด์— ๋งค์นญ ๋˜๋Š” ์ˆซ์ž๋“ค์ด ์ž…๋ ฅ๋  ๋•Œ ํ›„์œ„ ํ‘œ๊ธฐ์‹์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

  • `string.ascii_uppercase`๋ฅผ ํ™œ์šฉํ•˜์—ฌ A - Z์— ๋Œ€์‘๋˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
    • `stirng.ascii_uppercase`๋Š” ์‚ฌ์ „์— ์ •์˜๋œ string ์ƒ์ˆ˜๋กœ ABCDEFGHIJKLMNOPQRSTUVWXYZ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • ์ž…๋ ฅ๋œ ๊ฐ’์ด ์•ŒํŒŒ๋ฒณ์ธ ๊ฒฝ์šฐ stack์— pushํ•˜๊ณ  ์—ฐ์‚ฐ์ž๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋‹ค์‹œ stack์— ์ถ”๊ฐ€ํ•œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
import string

if __name__ == "__main__":
    change = dict()
    stack = []
    num_operand = int(stdin.readline())
    math_expr = stdin.readline()[:-1]

    # ์•ŒํŒŒ๋ฒณ๊ณผ ๋งค์นญ๋˜๋Š” ์ˆซ์ž๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
    for alpha in string.ascii_uppercase[:num_operand]:
        change[alpha] = int(stdin.readline())

    for c in math_expr:
        if c.isalpha():
            stack.append(change[c])
        else:
            n2, n1 = stack.pop(), stack.pop()
            if c == '*':
                stack.append(n1 * n2)
            elif c == '/':
                stack.append(n1 / n2)
            elif c == '+':
                stack.append(n1 + n2)
            else:
                stack.append(n1 - n2)

    print("%.2f" % stack[-1])
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€