Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 나이순 정렬 #213

Closed
hwangJi-dev opened this issue Apr 25, 2023 · 0 comments
Closed

[Algorithm] 나이순 정렬 #213

hwangJi-dev opened this issue Apr 25, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Apr 25, 2023

💬 문제

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant