๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ™‚ | Silver

[Kotlin, S5] ๋ฐฑ์ค€ 30010๋ฒˆ ์ž˜๋ชป๋œ ๋ฒ„๋ธ”์ •๋ ฌ

by immgga 2024. 8. 17.

์ถœ์ฒ˜: unsplash.com

 

์ž˜๋ชป๋œ ๋ฒ„๋ธ”์ •๋ ฌ(30010๋ฒˆ)

Silver 5

#์• ๋“œ ํ˜น #ํ•ด ๊ตฌ์„ฑํ•˜๊ธฐ

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

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

์œ„ ๋ฌธ์ œ์— ์ž˜๋ชป๋œ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„๋œ ๋ฒ„๋ธ” ์ •๋ ฌ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

์ด ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ฒ„๋ธ” ์ •๋ ฌ์„ ํ–ˆ์„ ๋•Œ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ •๋ ฌ๋˜์ง€ ์•Š๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

๋จผ์ € ๋ฒ„๋ธ” ์ •๋ ฌ์ด ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ง€๋Š”์ง€ ์•Œ์•„์•ผ ํ•œ๋‹ค.

๋ฒ„๋ธ” ์ •๋ ฌ์€ ํ˜„์žฌ ์œ„์น˜์—์„œ ์ž์‹ ์˜ ์•ž์˜ ๊ฐ’์„ ํ™•์ธํ•ด์„œ ์ž์‹ ๋ณด๋‹ค ๊ฐ’์ด ์ž‘์„ ๊ฒฝ์šฐ ์„œ๋กœ์˜ ๊ฐ’์„ ๊ตํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„์„ ํ•œ๋‹ค.

๋ฌผ๋ก  1๋ฒˆ์˜ ์‚ฌ์ดํด๋กœ๋Š” ์ •๋ ฌ์ด ๋ฐ”๋กœ ์•ˆ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌํ•  List์˜ ํฌ๊ธฐ๋งŒํผ ๋ฐ˜๋ณตํ•ด ์ฃผ๋ฉด ์ •๋ ฌ์„ ๋งˆ์น  ์ˆ˜ ์žˆ๋‹ค.

์ถœ์ฒ˜: https://gmlwjd9405.github.io/2018/05/06/algorithm-bubble-sort.html

 

size๊ฐ€ 5์ธ List๋ฅผ 4๋ฒˆ์˜ ์‚ฌ์ดํด์„ ๋Œ์•„ ์ •๋ ฌ์„ ์™„๋ฃŒํ•œ ๋ถ€๋ถ„์ด๋‹ค.

ํ˜„์žฌ ์ž์‹ ์˜ ์•ž์˜ ๊ฐ’์„ ํ™•์ธํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•ž์˜ ๊ฐ’์ด ํ˜„์žฌ ์ž์‹ ๋ณด๋‹ค ๋” ์ž‘์œผ๋ฉด ๊ฐ’์„ ์„œ๋กœ ๊ตํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

์ž์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜์˜ ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ฐธ๊ณ ํ•ด์„œ ๋ฌธ์ œ์˜ ์ฝ”๋“œ๊ฐ€ ์ž˜๋ชป๋œ ์›์ธ์„ ๋ถ„์„ํ•ด ๋ณด์ž.

https://gmlwjd9405.github.io/2018/05/06/algorithm-bubble-sort.html

 

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋ฒ„๋ธ” ์ •๋ ฌ(bubble sort)์ด๋ž€ - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

 

๋ฌธ์ œ์˜ ์ฝ”๋“œ๋Š” ๊ฐ’ ํ™•์ธ์„ ๋งˆ์ง€๋ง‰ index๋ถ€ํ„ฐ 0๋ฒˆ์งธ index๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ณ  ์žˆ๋‹ค.

๋‚ด๋ถ€ ๋ฐ˜๋ณต๋ฌธ์—์„œ ์ด๋ฏธ ์ •๋ ฌ๋œ ๊ฐ’์ธ i๋ฒˆ์งธ index ์ด์ „๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ๋„ ์ž˜ ๊ตฌํ˜„ํ–ˆ๋‹ค. ์•ž์˜ ๊ฐ’์ด ๋” ์ž‘์€ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•˜๋Š” ๊ฒƒ๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.

 

๋ฌธ์ œ์˜ ์ฝ”๋“œ๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด ๋ฌธ์ œ์ ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์‚ฌ์ดํด์˜ ์ˆœ์„œ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ๋ณด๊ณ  ์žˆ๋Š” ๊ฑด ์ƒ๊ด€์—†๋‹ค.

ํ•˜์ง€๋งŒ ์‚ฌ์ดํด์„ ์—ญ์ˆœ์œผ๋กœ ๋ณด๊ณ  ์žˆ๋Š”๋ฐ ์•ˆ์ชฝ ๋ฐ˜๋ณต๋ฌธ์€ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์ œ์™ธ์‹œํ‚ค๊ณ  ์žˆ๋‹ค.

์‚ฌ์ดํด์ด ์—ญ์ˆœ์œผ๋กœ ๋Œ๊ณ  ์žˆ์œผ๋ฉด ๋‚ด๋ถ€์˜ ๋ฐ˜๋ณต๋ฌธ๋„ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด ์•„๋‹Œ ์ฒซ ๋ฒˆ์งธ ๊ฐ’๋ถ€ํ„ฐ ์ œ์™ธ์‹œ์ผฐ์–ด์•ผ ์ •๋ ฌ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

 

N = 5์ผ ๋•Œ

5 4 3 2 1

1 5 4 3 2	// i = 4, j = 3
1 3 5 4 2	// i = 3, j = 2
1 3 5 4 2	// i = 2, j = 1
1 3 5 4 2	// i = 1, j = 0

์•„๋ž˜ N์ด 5์ผ ๋•Œ ๋ฐ˜๋ณต์— ๋”ฐ๋ฅธ i๊ณผ j๊ฐ’๊ณผ ์ •๋ ฌ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜์—ดํ•œ ๊ฒฐ๊ณผ์ด๋‹ค.

j์˜ ๊ฐ’์€ i์˜ ๊ฐ’์— ์˜ํ•ด ๋ณ€๊ฒฝ๋œ๋‹ค. ์•ˆ์ชฝ ๋ฐ˜๋ณต์€ j๋ฒˆ์งธ index๋ถ€ํ„ฐ 0๊นŒ์ง€ ๋ฐ˜๋ณต๋œ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์‚ฌ์ดํด์—์„œ๋Š” 3๋ฒˆ์งธ index๋ถ€ํ„ฐ 0๋ฒˆ์งธ index๊นŒ์ง€ ํ™•์ธํ–ˆ๋‹ค. ์ฒซ ์‚ฌ์ดํด์—์„œ๋Š” ๋‚ด๋ถ€ ๋ฐ˜๋ณต๋ฌธ ๋‚ด๋ถ€์—์„œ j + 1์„ ์ด์šฉํ•ด ๋งˆ์ง€๋ง‰์ธ 4๋ฒˆ์งธ index๊นŒ์ง€ ํ™•์ธํ–ˆ์œผ๋ฏ€๋กœ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.

๋‘ ๋ฒˆ์งธ ์‚ฌ์ดํด์—์„œ๋Š” j๊ฐ€ 2์ด๊ธฐ ๋•Œ๋ฌธ์— j + 1์„ ํ•ด๋„ 3๋ฒˆ์งธ index๊นŒ์ง€๋ฐ–์— ๋ณผ ์ˆ˜ ์—†๋‹ค. ํ˜„์žฌ ์ •๋ ฌ์„ ์ž‘์€ ๊ฐ’์„ ์•ž์œผ๋กœ ๋ณด๋‚ด๋Š” ์ •๋ ฌ์„ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๋งˆ์ง€๋ง‰ index๋ฅผ ์ œ์™ธ์‹œํ‚ค๊ณ  ์žˆ์œผ๋‹ˆ ์ •๋ ฌ์ด ์ œ๋Œ€๋กœ ๋  ๋ฆฌ๊ฐ€ ์—†๋‹ค.

 

์ด๋กœ ์ธํ•ด ์œ„ ์ฝ”๋“œ์˜ ๋ฐ˜๋ก€๋Š” ๊ฐ€์žฅ ํฐ ๊ฐ’์ด 0๋ฒˆ์งธ index๋กœ ์˜ฌ ๊ฒฝ์šฐ, ์‚ฌ์ดํด์„ ๋Œ๋ฉด์„œ 1์นธ์”ฉ ๋’ค๋กœ ์ด๋™ํ•œ๋‹ค๊ณ  ํ•ด๋„ ์ œ์™ธ๋œ ๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ์ œ์ผ ํฐ ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ๋’ค๋กœ ์ด๋™ํ•˜์ง€ ๋ชปํ•œ๋‹ค. ์œ„์˜ N = 5์ผ ๋•Œ์˜ ์˜ˆ์ œ๋„ ๊ทธ๋ ‡์ง€ ์•Š์€๊ฐ€?

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val size = readLine().toInt()
    val result = StringBuilder()

    for (i in size downTo 1) {
        result.append("$i ")
    }

    println(result)
}

 

๋ฌธ์ œ ํ’€์ด

๋ฌธ์ œ ์ ‘๊ทผ์—์„œ ํŒŒ์•…ํ•œ ๋ฌธ์ œ๋กœ๋Š” 0๋ฒˆ์งธ index์— ์ œ์ผ ํฐ ๊ฐ’์ด ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋ฉด ๋ฌธ์ œ์˜ ์ฝ”๋“œ๋กœ๋Š” ์ •๋ ฌ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ ๋˜์—ˆ์œผ๋‹ˆ, size์˜ ์—ญ์ˆœ์œผ๋กœ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ์ฃผ๋ฉด ์ •๋‹ต์ด ๋œ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

๋ฒ„๋ธ” ์ •๋ ฌ ์ดํ•ด๋„ ํ…Œ์ŠคํŠธ.

๋ฒ„๋ธ” ์ •๋ ฌ์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ํŒŒ์•…ํ•ด ๋ฌธ์ œ ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ ์„ ํŒŒ์•…ํ•ด ์˜ฌ๋ฐ”๋ฅธ ๋ฐ˜๋ก€๋ฅผ ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค.

๋‚˜๋Š” ๋ฒ„๋ธ” ์ •๋ ฌ์˜ ๊ฐœ๋…์„ ์ฐพ์•„๋ณด๊ณ  ๋‚˜์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

๊ทธ๋ƒฅ ๋ฌธ์ œ์ ์„ ํŒŒ์•…ํ•˜์ง€ ์•Š๊ณ  ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ’์„ shuffle ํ•ด์„œ ๋žœ๋ค ํ•˜๊ฒŒ ๊ฐ’์„ ๋“ค์–ด๊ฐ€๊ฒŒ ํ•œ ํ›„, ์ •๋ ฌ์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ด๋„ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๋ฒ„๋ธ” ์ •๋ ฌ์˜ ํŠน์ง•์„ ์ž˜ ํŒŒ์•…ํ•ด์„œ ๋ฌธ์ œ์ ์„ ์ฐพ์•„์„œ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๊ณต๋ถ€์— ๋” ๋„์›€์ด ๋  ๊ฒƒ์ด๋‹ค.

728x90