Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1935๋ฒˆ - ํ›„์œ„ ํ‘œ๊ธฐ์‹2

๊ฐ์ž ๐Ÿฅ” 2022. 2. 11. 18:05
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

https://www.acmicpc.net/problem/1935

 

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

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

www.acmicpc.net

 

ํ›„์œ„ ํ‘œ๊ธฐ์‹์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ์ž˜ ์•Œ์ง€ ๋ชปํ•˜์—ฌ ์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ณด๊ณ , ํ›„์œ„ํ‘œ๊ธฐ์‹์ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๊ณ„์‚ฐ๋˜๋Š”์ง€ ๋จผ์ € ์‚ดํŽด๋ณด์•˜๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ stack ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•œ๋‹ค๋Š” ์ ์ด ํŠน์ง•์ด์—ˆ๋‹ค. ์ปดํ“จํ„ฐ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ค‘์œ„ํ‘œ๊ธฐ์‹ (1+2=3) ๋ณด๋‹ค ํ›„์œ„ํ‘œ๊ธฐ์‹์ด ๋” ์ดํ•ด๊ฐ€ ์‰ฌ์šด์ง€ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.๊ณ„์†ํ•ด์„œ ๊ฒฐ๊ด๊ฐ’์„ stack์— ์ €์žฅํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด ์•„๋‹๊นŒ.

๋‚ด๊ฐ€ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•˜๊ธฐ ์œ„ํ•ด ์ฐธ๊ณ ํ•œ ํ›„์œ„ํ‘œ๊ธฐ์‹ ๊ฐœ๋…๊ธ€์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

https://todaycode.tistory.com/73?category=997273 

 

์ค‘์œ„ ํ‘œ๊ธฐ๋ฒ•๊ณผ ํ›„์œ„ ํ‘œ๊ธฐ๋ฒ•

1. ๊ฐœ๋…  1-1. ์ค‘์œ„ ํ‘œ๊ธฐ๋ฒ•์ด๋ž€?  1-2. ํ›„์œ„ ํ‘œ๊ธฐ๋ฒ•์ด๋ž€? 2. ์ค‘์œ„ ํ‘œ๊ธฐ์‹์„ ํ›„์œ„ ํ‘œ๊ธฐ์‹์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋ฒ•  2-1. ๊ด„ํ˜ธ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ  2-2. ๊ด„ํ˜ธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ 3. ๊ณ„์‚ฐ  3-1. ํ›„์œ„ ํ‘œ๊ธฐ์‹์„ ์‚ฌ์น™์—ฐ์‚ฐํ•˜๋Š” ๋ฒ• 1.

todaycode.tistory.com

 

์œ„ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋ฐฑ์ค€ 1935๋ฒˆ์˜ ์˜ˆ์ œ ์ž…๋ ฅ 1๋ฒˆ์„ ํ’€์–ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค!

 

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด

  • ์ž…๋ ฅ๋ฐ›๋Š” nums ์— ๋ฌด์กฐ๊ฑด์ ์œผ๋กœ 1,2,3,4 ..... ๊ฐ€ ์ฐจ๋ก€๋กœ ๋“ค์–ด์˜จ๋‹ค๊ณ ๋งŒ ์ƒ๊ฐํ•˜์—ฌ ๋งˆ์ง€๋ง‰์— ์•„์Šคํ‚ค์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ 64๋ฅผ ๋บ€ ์ˆ˜๋ฅผ stack์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. ๋‹น์—ฐํžˆ ์˜ค๋ฅ˜์˜€๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ 1.2.3.4.5 ๋ฅผ ๋„ฃ์ง€ ์•Š๊ณ  2,45,677, .. ์ด๋ ‡๊ฒŒ ๋ฌด์ž‘์œ„๋กœ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊นจ๋‹ซ๊ณ  ์ž…๋ ฅ๋ฐ›์•„๋†“์€ nums์˜ ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•˜์—ฌ ๊ฐ’์„ stack์— ๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค.
  • ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ๋“ค์€ ๊ตฌํ˜„ํ•˜๊ธฐ ์‰ฌ์› ๋‹ค.
  • ๋‹จ ์œ„์—์„œ ๊ทธ๋ฆผ์œผ๋กœ ๊ทธ๋ฆฐ ์Šคํƒ์„ ๋ณด๋ฉด, - ์™€ / ๋Š” ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. (์—ฌ๊ธฐ์„œ๋„ ํ—ค๋งธ๋‹ค.)
    • 2์™€ 3์„ ๊ณฑํ• ๋•Œ ๋จผ์ €๊บผ๋‚ธ 3์—๋‹ค๊ฐ€ 2๋ฅผ ๊ณฑํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, ๋’ค๋Šฆ๊ฒŒ๊บผ๋‚ธ 2์—๋‹ค๊ฐ€ 3์„ ๊ณฑํ–ˆ๋‹ค.
    • ๋’ท๋ถ€๋ถ„์— 4์™€ 5๋ฅผ ๊บผ๋‚ด์–ด ๋‚˜๋ˆŒ๋•Œ๋„, 5๋ฅผ ๋จผ์ €๊บผ๋‚ด์–ด 5์—๋‹ค๊ฐ€ 4๋ฅผ ๋‚˜๋ˆ„๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋‚˜์ค‘์—๊บผ๋‚ธ 4์—๋‹ค๊ฐ€ ๋จผ์ €๊บผ๋‚ธ 5๋ฅผ ๋‚˜๋ˆด๋‹ค. ์ด์ฒ˜๋Ÿผ ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค.
    • ํ•˜์ง€๋งŒ ๋ง์…ˆ๊ณผ ๊ณฑ์…ˆ์€ ์ˆœ์„œ๊ฐ€ ์˜๋ฏธ๊ฐ€ ์—†๊ธฐ์—, ๋‚˜๋ˆ—์…ˆ๊ณผ ๋บ„์…ˆ์—๋งŒ ์ˆœ์„œ๋ฅผ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ„๋„์˜ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•œ ํ›„, ๊ณ„์‚ฐ์„ ์‹คํ–‰ํ–ˆ๋‹ค.
import Foundation

let n = Int(readLine()!)!
var orderLine = readLine()!
var nums: [Double] = []
var stack: [Double] = []

for _ in 0..<n {
    let num = Double(readLine()!)!
    nums.append(num)
}

for order in orderLine {
    if order == "+" {
        stack.append(stack.removeLast()+stack.removeLast())
    } else if order == "-" {
        let a = stack.removeLast()
        stack.append(stack.removeLast() - a)
    } else if order == "*" {
        stack.append(stack.removeLast() * stack.removeLast())
    } else if order == "/" {
        let b = stack.removeLast()
        stack.append(stack.removeLast() / b)
    } else {
        let idx = order.asciiValue! - 65
        stack.append(nums[Int(idx)])
    }
}

print(String(format: "%.2f", stack[0]))

 

๋ฐ˜์‘ํ˜•