[백준] Words (4072)(kotlin)

문제 설명

백준 4072번 문제 링크

입력 및 출력

» 입력

Input will consist of a number of lines, each containing up to 250 characters. Words will be separated by single spaces, i.e. not by tabs, double spaces or other characters. Words may be of any length. Input will be terminated by a line containing a single #.

» 출력

Output will consist of one line for each line of input. In the output line, each word (as defined in paragraph 2 above) in the input line will be reversed.

예제 입출력(테스트케이스)

입력 출력
teG ydaer rof eht weN dnalaeZ gnimmargorP tsetnoC no .ht41
I like ice-cream.
#
Get ready for the New Zealand Programming Contest on 14th.
I ekil .maerc-eci

문제 풀이1

fun main(args: Array<String>) = with(System.`in`.bufferedReader()) {
    val files = mutableListOf<String>()
    val n = readLine().toInt()

    if (n == 1) {
        println(readLine())
        return
    }
    else {
        repeat(n) {
            files.add(readLine())
        }
    }

    var check = BooleanArray(files[0].length) { true }
    for (i in files[0].indices) for (j in 1 until n ) {
        if (check[i]) check[i] = files[0][i] == files[j][i]
    }

    println(
        check.mapIndexed { i: Int, b: Boolean ->
            if (b) files[0][i]
            else '?'
        }.joinToString("")
    )
}

문제 풀이2

fun main(args: Array<String>) = with(System.`in`.bufferedReader()) {
    while (true) {
        val str = readLine()
        if (str[0] == '#') break
        println(str.split(" ").joinToString(" ") { it.reversed() })
    }
}