ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Kotlin ๐Ÿฌ ๋ฐฑ์ค€ 16๋‹จ๊ณ„ :: 29278 ๋ฒˆ
    2025. 3. 30. 14:20
    ๋ฐ˜์‘ํ˜•

    ์Šคํƒ2

    ๋ฌธ์ œ   |

      ์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ์Šคํƒ์„ ๊ตฌํ˜„ํ•œ ๋‹ค์Œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋ช…๋ น์€ ์ด ๋‹ค์„ฏ ๊ฐ€์ง€์ด๋‹ค.

    1. 1 X: ์ •์ˆ˜ X๋ฅผ ์Šคํƒ์— ๋„ฃ๋Š”๋‹ค. (1 ≤ X ≤ 100,000)
    2. 2: ์Šคํƒ์— ์ •์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ๋งจ ์œ„์˜ ์ •์ˆ˜๋ฅผ ๋นผ๊ณ  ์ถœ๋ ฅํ•œ๋‹ค. ์—†๋‹ค๋ฉด -1์„ ๋Œ€์‹  ์ถœ๋ ฅํ•œ๋‹ค.
    3. 3: ์Šคํƒ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    4. 4: ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
    5. 5: ์Šคํƒ์— ์ •์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ๋งจ ์œ„์˜ ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์—†๋‹ค๋ฉด -1์„ ๋Œ€์‹  ์ถœ๋ ฅํ•œ๋‹ค.

    ์ž…๋ ฅ   |

      ์ฒซ์งธ ์ค„์— ๋ช…๋ น์˜ ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000,000)

      ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ ์ค„์— ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค.

      ์ถœ๋ ฅ์„ ์š”๊ตฌํ•˜๋Š” ๋ช…๋ น์€ ํ•˜๋‚˜ ์ด์ƒ ์ฃผ์–ด์ง„๋‹ค.

     

    ์ถœ๋ ฅ   |

      ์ถœ๋ ฅ์„ ์š”๊ตฌํ•˜๋Š” ๋ช…๋ น์ด ์ฃผ์–ด์งˆ ๋•Œ๋งˆ๋‹ค ๋ช…๋ น์˜ ๊ฒฐ๊ณผ๋ฅผ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    ํ’€์ด  |

      java util Stack ๋ฐ ์†๋„ ์ด์Šˆ๋กœ BufferedReader ์‚ฌ์šฉ

     

     

    ๋‹ต์•ˆ  |

    import java.util.Stack
    import java.io.BufferedReader
    import java.io.InputStreamReader
    
    fun main() {
    	val br = BufferedReader(InputStreamReader(System.`in`))
    	val sb = StringBuilder()
        val N = br.readLine().toInt()
        val X = Stack<Int>()
    
        repeat(N) {
            val line = br.readLine().split(" ")
            when (line[0]) {
                "1" -> X.push(line[1].toInt())
                "2" -> sb.appendLine(if (X.isEmpty()) -1 else X.pop())
                "3" -> sb.appendLine(X.size)
                "4" -> sb.appendLine(if (X.isEmpty()) 1 else 0)
                "5" -> sb.appendLine(if (X.isEmpty()) -1 else X.peek())
            }
        }
        print(sb)
    }
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.