코딩테스트

나이순 정렬(백준)

dofury 2023. 7. 10. 12:34
728x90

package com.example.algorithm

import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter



fun main(){

    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.`out`))

    val lists = mutableListOf<Pair<Int,String>>()
    val count = br.readLine().toInt()
    repeat(count){
        val input = br.readLine().split(' ')
        lists.add(Pair(input[0].toInt(),input[1]))
    }
    lists.sortBy {
        it.first
    }

    lists.map {
        bw.write(String.format("%d %s",it.first,it.second))
        bw.newLine()
    }

    bw.flush()
    bw.close()
    br.close()
}

 

처음에는 map으로 접근하였으나 동명이인에 대한 처리가 힘들것 같아서

리스트로 풀었다.

728x90