๐ŸฏAlgorithm ๊ฟ€ํŒ with Java & Kotlin

: ) YOUNGยท2023๋…„ 7์›” 12์ผ
2

์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ชฉ๋ก ๋ณด๊ธฐ
226/417
post-thumbnail

1. ๋งŽ์€ ๋ถ„๊ธฐ ์กฐ๊ฑด์„ ๊ฐ€์งˆ ๋•Œ๋Š” if else ๋ณด๋‹ค๋Š” Switch (Kotlin์—์„œ๋Š” when)๋ฅผ ์‚ฌ์šฉํ•˜์ž

if else๋ณด๋‹ค๋Š” ์‹ค์ œ๋กœ switch๊ฐ€ ๋” ์ตœ์ ํ™”์— ๋„์›€์ด ๋œ๋‹ค. ๋ฌผ๋ก  ์ ์€ ์กฐ๊ฑด์—์„œ๋Š” ์ฐจ์ด๊ฐ€ ๋ณ„๋กœ ์—†๊ฒ ์ง€๋งŒ, ๋ฌธ์ œ์— ๋”ฐ๋ผ์„œ ์กฐ๊ฑด ๋ถ„๊ธฐ๋ฅผ ๋งŽ์ด ํ•ด์•ผ๋˜๋Š” ๊ตฌํ˜„ ๋ฌธ์ œ๋ผ๋˜๊ฐ€ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ์—์„œ๋Š” switch๊ตฌ๋ฌธ์ด ์ตœ์ ํ™”์— ๋„์›€์ด ๋œ๋‹ค.

Ex)


private fun solve(a: Int, b: Int): Int {
    if(a + b == 2) {
        return 2
    } else {
        return 1
    }
} // End of solve

private fun solve(a: Int, b: Int): Int {
    when(a + b) {
        1 -> return 1
        2 -> return 3
        else -> return 4
    }
} // End of solve


2. List๋‚˜ ๋ฐฐ์—ด์„ ๋ฐ˜๋ณตํ• ๋•Œ for๋ฌธ ๋‚ด๋ถ€์— ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ์„ ์ž์ œํ•˜์ž


	val testList = arrayOf(1, 2, 3)

    for (i in 0 until testList.size) {
        println(testList[i])
    }

์ „์ฒด๋ฅผ ๋ฐ˜๋ณตํ•ด์•ผ ํ• ๋•Œ for๋ฌธ ๋‚ด๋ถ€์— size๋‚˜ length()๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ for ๋ฌธ์„ ๋Œ๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€๋ฐ, ์ด๋Ÿด ๊ฒฝ์šฐ for๋ฌธ์ด ํ•œ๋ฒˆ ๋Œ๊ณ  ๋‚œ ํ›„ testList์˜ size๊ฐ€ ์–ผ๋งˆ์ธ์ง€ ํ™•์ธํ•˜๊ณ , ๋‹ค์‹œ for๋ฌธ์˜ ์กฐ๊ฑด์ด true์ธ์ง€ ํ™•์ธํ•˜๊ณ  ๋ฐ˜๋ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— for๋ฌธ์„ ํ•œ๋ฒˆ ๋ฐ˜๋ณตํ•  ๋•Œ ๋งˆ๋‹ค ๋ฉ”์†Œ๋“œ๋ฅผ ๋“ค์–ด๊ฐ”๋‹ค๊ฐ€ ๋‚˜์˜ค๊ฒŒ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

์ด๋Ÿด ๋•Œ๋Š” ๋ฆฌ์ŠคํŠธ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ณ€์ˆ˜๋กœ ๋นผ์„œ size๋Š” ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด๋‘๊ณ , for๋ฌธ ๋‚ด๋ถ€์—์„œ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์•ฝ๊ฐ„์˜ ์ตœ์ ํ™”์— ๋„์›€์ด ๋œ๋‹ค.


    val size = testList.size
    for(i in 0 until size) {
        println(testList[i])
    }


3. println() ๋ณด๋‹ค๋Š” BufferedWriter๋ฅผ ์‚ฌ์šฉํ•˜์ž

	
    println()

    val bw = BufferedWriter(OutputStreamWriter(System.out))
    bw.write("")
    bw.close()
    

์ฐธ๊ณ ๋กœ BufferedWriter์—์„œ

close() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ž๋™์œผ๋กœ flush()๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— write(), flush(), close() 3๊ฐœ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

๋˜ํ•œ, Java์—์„œ๋Š” Exception์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ๋˜๋Š”๋ฐ, ๊ทธ๋ƒฅ throwํ•ด์ฃผ๋ฉด ๋œ๋‹ค.


4. ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์„ ํ•  ๋•Œ๋Š” +๋ณด๋‹ค StringBuilder์™€ append()๋ฅผ ์‚ฌ์šฉํ•˜์ž

    
    var ans = ""
    ans = "์ •" + "๋‹ต"
    println(ans)

๋ฌธ์ž์—ด ์—ฐ์‚ฐ์€ ์œ„ ๋ฐฉ์‹์œผ๋กœ๋„ ๊ฐ€๋Šฅํ•œ๋ฐ, ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์˜ ๋ฐฉ๋ฒ•์ด ๋งค์šฐ ํšจ์œจ์ ์ด๋‹ค.

	
    var sb = StringBuilder()
    val bw = BufferedWriter(OutputStreamWriter(System.out))
    sb.append("์ •๋‹ต").append("์ž…๋‹ˆ๋‹ค.")
    bw.write(sb.toString())
    bw.close()

StringBuilder์™€ BufferedWirter๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ํ›จ์”ฌ ๋” ์ตœ์ ํ™”์— ๋„์›€์„ ์ฃผ์–ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.


๋˜ํ•œ StringBuilder์—์„œ append()๋ฅผ ํ•  ๋•Œ ํ•œ๊ธ€์ž ๋‹จ์œ„์ผ ๊ฒฝ์šฐ "๋ฅผ ์‚ฌ์šฉํ•ด์„œ String ํƒ€์ž…๋ณด๋‹ค๋Š” '๋ฅผ ์‚ฌ์šฉํ•ด์„œ Char ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.


// ์ค„๋ฐ”๊ฟˆ์„ ํ•ด์•ผ ํ•  ๋•Œ,
    var sb = StringBuilder()
    val bw = BufferedWriter(OutputStreamWriter(System.out))
    sb.append("์ •๋‹ต").append("์ž…๋‹ˆ๋‹ค.").append('\n')
    bw.write(sb.toString())
    bw.close()

.append('\n') ์˜ ํ˜•์‹ ์ฒ˜๋Ÿผ ํ•œ๊ธ€์ž์ธ Char ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์€ ์‹ฑ๊ธ€ ์ฟผํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ append ํ•  ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.




๋ฐฑ์ค€์—์„œ๋Š” ๋ฌธ์ œ๋งˆ๋‹ค ๋ฉ”๋ชจ๋ฆฌ์™€ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์ˆœ์œ„๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ, ์Šน๋ถ€์š•์ด ๋งŽ์€ ๋‚˜๋Š”... ์กฐ๊ธˆ์ด๋ผ๋„ ๋“ฑ์ˆ˜๋ฅผ ์˜ฌ๋ ค๋ณด๊ธฐ ์œ„ํ•ด ๋งž์€ ๋ฌธ์ œ๋„ ๋‹ค์‹œ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ์ตœ์ ํ™”๋ฅผ ๊ณ ๋ฏผํ•ด๋ณด๋Š” ์Šต๊ด€์„ ๊ฐ€์ง€๊ฒŒ ๋˜์—ˆ๋‹ค

์ด๋Ÿฐ ์Šต๊ด€ ๋•๋ถ„์— ๋งŽ์€ ๋ฐœ์ „์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ„๊ธฐ๊ฐ€ ๋˜์—ˆ๊ณ , ๋‹ค๋ฅธ ๋ถ„๋“ค์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด์„œ ์ฝ”๋“œ๋ฆฌ๋ทฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ๋„ ๊ฐ€์ง€๊ฒŒ ๋˜์—ˆ๋‹ค.

์‚ฌ์‹ค ๋ณด๊ธฐ์— ๋ณ„๊ฑฐ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์ง€๋งŒ ์ด๋Ÿฐ ์ž‘์€ ๊ตฌํ˜„์˜ ์ฐจ์ด์—์„œ ์˜ค๋Š” ๊ฒƒ๋“ค์ด ๋ชจ์—ฌ์„œ ํ”„๋กœ๊ทธ๋žจ์˜ ํฐ ์ตœ์ ํ™”์— ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ธฐ ๋•Œ๋ฌธ์— ๋” ๋งŽ์€ ํฅ๋ฏธ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค

์˜ค๋Š˜์˜ ๊ตํ›ˆ ๊บผ์ง„ ์ฝ”๋“œ๋„ ๋‹ค์‹œ๋ณด์ž!


์ข‹์•„์š” ๋ˆ„๋ฅด๊ณ  ๊ฐ€๋ผ!

0๊ฐœ์˜ ๋Œ“๊ธ€