• Home
  • About
    • Moon photo

      Moon

      개발자는 자고 싶다.

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Posts
    • All Posts
    • All Tags
  • Projects

백준 - 2798 블랙잭

05 Jun 2022

Reading time ~1 minute

문제

2798 블랙잭

screencaptures

답

kotlin code

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)
}


baekjoonkotlin코틀린백준브론즈브루트포스 알고리즘 Share Tweet +1