ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Kotlin ๐Ÿฌ ๋ฐฑ์ค€ 4๋‹จ๊ณ„ :: 10813 ๋ฒˆ
    2023. 4. 7. 15:45
    ๋ฐ˜์‘ํ˜•

    ๊ณต ๋ฐ”๊พธ๊ธฐ

    ๋ฌธ์ œ   |

      ๋„ํ˜„์ด๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ด N๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค. ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” ๊ณต์ด 1๊ฐœ์”ฉ ๋“ค์–ด์žˆ๊ณ , ์ฒ˜์Œ์—๋Š” ๋ฐ”๊ตฌ๋‹ˆ์— ์ ํ˜€์žˆ๋Š” ๋ฒˆํ˜ธ์™€ ๊ฐ™์€ ๋ฒˆํ˜ธ๊ฐ€ ์ ํžŒ ๊ณต์ด ๋“ค์–ด์žˆ๋‹ค.

      ๋„ํ˜„์ด๋Š” ์•ž์œผ๋กœ M๋ฒˆ ๊ณต์„ ๋ฐ”๊พธ๋ ค๊ณ  ํ•œ๋‹ค. ๋„ํ˜„์ด๋Š” ๊ณต์„ ๋ฐ”๊ฟ€ ๋ฐ”๊ตฌ๋‹ˆ 2๊ฐœ๋ฅผ ์„ ํƒํ•˜๊ณ , ๋‘ ๋ฐ”๊ตฌ๋‹ˆ์— ๋“ค์–ด์žˆ๋Š” ๊ณต์„ ์„œ๋กœ ๊ตํ™˜ํ•œ๋‹ค.

      ๊ณต์„ ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ€์ง€๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, M๋ฒˆ ๊ณต์„ ๋ฐ”๊พผ ์ดํ›„์— ๊ฐ ๋ฐ”๊ตฌ๋‹ˆ์— ์–ด๋–ค ๊ณต์ด ๋“ค์–ด์žˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

     

    ์ž…๋ ฅ   |

      ์ฒซ์งธ ์ค„์— N (1 ≤ N ≤ 100)๊ณผ M (1 ≤ M ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค.

      ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๊ณต์„ ๊ตํ™˜ํ•  ๋ฐฉ๋ฒ•์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๋ฐฉ๋ฒ•์€ ๋‘ ์ •์ˆ˜ i j๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, i๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์™€ j๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์— ๋“ค์–ด์žˆ๋Š” ๊ณต์„ ๊ตํ™˜ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. (1 ≤ i ≤ j ≤ N)

      ๋„ํ˜„์ด๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๊ณต์„ ๊ตํ™˜ํ•œ๋‹ค.

     

    ์ถœ๋ ฅ   |

      1๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ๋ถ€ํ„ฐ N๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์— ๋“ค์–ด์žˆ๋Š” ๊ณต์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    ํ’€์ด  |

      1. ์ž…๋ ฅ ๋ฐ›์€ ๋ฐ”๊ตฌ๋‹ˆ ๊ฐฏ์ˆ˜ N ํฌ๊ธฐ ๋งŒํผ์˜ int array ์ƒ์„ฑ

      2. ๋ฐ”๊ตฌ๋‹ˆ ์œ„์น˜ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” ๊ณต ๋ฒˆํ˜ธ ์ €์žฅ

      3. i๋ฒˆ๊ณผ j๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์˜ ๊ณต ๋ฒˆํ˜ธ ๊ตํ™˜

     

     

    ๋‹ต์•ˆ  |

    import java.util.Scanner
    
    fun main() = with(Scanner(System.`in`)) {
        val N = nextInt()
        val M = nextInt()
        var buckets = IntArray(N) { it + 1 }
        
        for (time in 0 until M) {
            val i = nextInt()
            val j = nextInt()
            
            val temp = buckets[i - 1]
            buckets[i - 1] = buckets[j - 1]
            buckets[j - 1] = temp
        }
        
        buckets.forEach { print("${it} ") }
    }
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.