Skip to content

[Algorithm] 나이순 정렬 #213

Closed
Closed
@hwangJi-dev

Description

@hwangJi-dev

💬 문제

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


💬 Idea

  • 딕셔너리에 나이를 key값으로 설정하고, 먼저 가입한 순서대로 value를 저장하기 위해 value는 array 타입으로 설정한다.
  • 딕셔너리를 나이가 작은 순으로 정렬한 뒤 해당 나이의 회원을 가입한 순서대로 출력한다.

💬 풀이

import Foundation

func solution10814() {
    let n = Int(readLine()!)!
    var ojDict: [Int: [String]] = [:]
    
    for _ in 1...n {
        let o = readLine()!.components(separatedBy: .whitespaces)
        let age = Int(o[0])!
        
        if ojDict[age] == nil {
            ojDict[age] = [o[1]]
        } else {
            ojDict[age]?.append(o[1])
        }
    }
    
    for i in ojDict.sorted(by: { $0.key < $1.key }) {
        for j in i.value {
            print("\(i.key) \(j)")
        }
    }
}

solution10814()

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions