728x90
λ°μν
Problem
Solution
1. λμ λ리λ₯Ό νμ©ν΄ μ μ μ 보λ₯Ό λ£λλ€.
keyκ°μΌλ‘ λμ΄λ₯Ό,valueκ°μΌλ‘ μ΄λ¦μ λ£μ΅λλ€.
λ§μ½ λμ λ리μ keyκ°μ΄ λΉμ΄μμ§ μλ€λ©΄ valueμ μΆκ°λ‘ append ν΄μ€λλ€.
var userDic:[Int:[String]] = [:]
let numberOfUser = Int(readLine()!)!
for _ in 0..<numberOfUser {
let input = String(readLine()!)
let split = input.split(separator: " ")
let age = Int(split[0])!
let name = "\(split[1])"
addUser(age, name)
}
func addUser(_ age:Int,_ name:String) {
if userDic[age] == nil {
userDic[age] = [name]
}else {
userDic[age]!.append(name)
}
}
2. λμ λ리λ₯Ό keyκ°μΌλ‘ μ λ ¬ν΄ μ μ μ 보λ₯Ό μΆλ ₯νλ€.
let sort = userDic.sorted{$0.key < $1.key}
for user in sort {
for name in user.value {
print("\(user.key) \(name)")
}
}
Source Code
728x90
λ°μν
'π Problem Solution' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Swift] HackerRank Solve Me First (0) | 2022.01.31 |
---|---|
[Swift] Baekjoon λΉλ°λ²νΈ λ°μνκΈ° 4569λ² (0) | 2022.01.31 |
[Swift] Baekjoon 12919λ² Aμ B 2 (0) | 2022.01.07 |
[Swift] Baekjoon ν°λ¦°λ둬 λ§λ€κΈ° 1254λ² (0) | 2022.01.06 |
[Swift] Baekjoon μ΅λκ° 2562λ² (0) | 2022.01.06 |
λκΈ