Kotlin/Algorithm Problems

<백준> 24.05.09에 푼 문제들

re트 2024. 5. 9. 11:49
728x90

1. 올림픽(Silver 5)

[백준]

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

[깃허브]

 

ForCodeKata/baekjoon 문제집/올림픽 at main · heesoo-park/ForCodeKata

Contribute to heesoo-park/ForCodeKata development by creating an account on GitHub.

github.com

제출 결과

작성한 코드는 다음과 같다.

import java.io.BufferedWriter
import java.io.OutputStreamWriter

data class MedalsOfCountry(
    val country: Int,
    val gold: Int,
    val silver: Int,
    val bronze: Int
) {
    companion object {
        fun mappingToFormat(input: String): MedalsOfCountry {
            val splitInput = input.split(' ').map { it.toInt() }
            return MedalsOfCountry(splitInput[0], splitInput[1], splitInput[2], splitInput[3])
        }
    }
}

private val infoList = mutableListOf<MedalsOfCountry>()

fun main() = with(System.`in`.bufferedReader()) {
    val bw = BufferedWriter(OutputStreamWriter(System.out))
    val (n, k) = readln().split(' ').map { it.toInt() }

    repeat(n) {
        infoList.add(MedalsOfCountry.mappingToFormat(readln()))
    }

    infoList.sortWith(compareBy<MedalsOfCountry> { -it.gold }.thenBy { -it.silver }
        .thenBy { -it.bronze })

    var rank = 0
    var pass = 0
    for (i in 0 until infoList.size) {
        if (i > 0) {
            if (!compareMedals(infoList[i], infoList[i - 1])) {
                rank++
                rank += pass
                pass = 0
            } else {
                pass++
            }
        } else {
            rank++
        }

        if (infoList[i].country == k) {
            break
        }
    }

    bw.write("$rank")
    bw.flush()
    bw.close()
}

fun compareMedals(c1: MedalsOfCountry, c2: MedalsOfCountry) =
    c1.gold == c2.gold && c1.silver == c2.silver && c1.bronze == c2.bronze

(비교하는 for문에서 0이 아니라 1부터 얍삽하게 하려다가 계속 퇴짜맞고 건실하게 0부터 시작함...)

(다중 비교 조건에서는 sortWith, compareBy, thenBy를..!!

 

2. 덩치(Silver 5)

[백준]

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

[깃허브]

 

ForCodeKata/baekjoon 문제집/덩치 at main · heesoo-park/ForCodeKata

Contribute to heesoo-park/ForCodeKata development by creating an account on GitHub.

github.com

제출 결과

작성한 코드는 다음과 같다.

import java.io.BufferedWriter
import java.io.OutputStreamWriter

data class BigInfo(
    val weight: Int,
    val height: Int
) {
    companion object {
        fun mappingToFormat(input: String): BigInfo {
            val splitInput = input.split(' ').map { it.toInt() }
            return BigInfo(splitInput[0], splitInput[1])
        }
    }
}

private lateinit var infoList: List<BigInfo>
private lateinit var rankList: IntArray

fun main() = with(System.`in`.bufferedReader()) {
    val bw = BufferedWriter(OutputStreamWriter(System.out))
    val n = readln().toInt()

    infoList = List(n) { BigInfo.mappingToFormat(readln()) }
    rankList = IntArray(n)

    for (i in 0 until n) {
        for (j in 0 until n) {
            if (i == j) continue

            if (compareBig(infoList[i], infoList[j])) {
                rankList[i]++
            }
        }
    }

    rankList.forEach {
        bw.write("${it + 1} ")
    }
    bw.flush()
    bw.close()
}

fun compareBig(p1: BigInfo, p2: BigInfo) = p1.weight < p2.weight && p1.height < p2.height
반응형

'Kotlin > Algorithm Problems' 카테고리의 다른 글

<백준> 24.05.14에 푼 문제들  (0) 2024.05.14
<백준> 24.05.13에 푼 문제들  (0) 2024.05.13
<백준> 24.05.08에 푼 문제들  (0) 2024.05.08
<백준> 24.05.07에 푼 문제들  (0) 2024.05.07
<백준> 파티(Gold 3)  (0) 2024.05.03