Home 백준 - 2798 블랙잭
Post
Cancel

백준 - 2798 블랙잭

문제

2798 블랙잭

screencaptures

kotlin code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fun main() {
    val target: Int
    var max = 0
    readln().split(" ").map { it.toInt() }.let { target = it.last() }
    val cards = readln().split(" ").map { it.toInt() }.filter { it < target }.sortedDescending().toIntArray()

    for(i in 0 .. cards.lastIndex) {
        for(j in i + 1 ..cards.lastIndex) {
            val sumTwo = cards[i] + cards[j]
            if(sumTwo >= target) continue
            for (z in j + 1..cards.lastIndex) {
                val sum = sumTwo + cards[z]
                if(sum < max) break
                if(sum == target) {
                    println(sum)
                    return
                }
                if(sum in (max + 1)until  target) max = sum
            }
        }
    }
    println(max)
}
This post is licensed under CC BY 4.0 by the author.

-

백준 - 3009 네 번째 점