반응형
문제
https://www.acmicpc.net/problem/1755
풀이
간단한 구현 및 정렬문제이다.
Swift에 sort 메서드 안에서 클로저로 정렬하는 방법을 제시해 주는 방법으로 풀이하였다.
숫자를 문자로 변환하는 것은 dictionary를 쓰거나 enum을 사용하거나 여러 방법으로 할 수 있지만 enum으로 풀었다.
소스코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NumberType: String { | |
case one | |
case two | |
case three | |
case four | |
case five | |
case six | |
case seven | |
case eight | |
case nine | |
case zero | |
init?(num: Int) { | |
switch num { | |
case 0: self = .zero | |
case 1: self = .one | |
case 2: self = .two | |
case 3: self = .three | |
case 4: self = .four | |
case 5: self = .five | |
case 6: self = .six | |
case 7: self = .seven | |
case 8: self = .eight | |
case 9: self = .nine | |
default: return nil | |
} | |
} | |
} | |
let input = readLine()!.split { $0 == " " }.map { Int($0)! } | |
let m = input[0], n = input[1] | |
let array = [Int](m...n).sorted { | |
let a = "\($0)".map { Int(String($0))! } | |
let b = "\($1)".map { Int(String($0))! } | |
return a.map { NumberType(num: $0)!.rawValue }.joined() < b.map { NumberType(num: $0)!.rawValue }.joined() | |
} | |
for i in 0..<array.count { | |
if i % 10 == 9 { | |
print(array[i]) | |
} else { | |
print(array[i], terminator: " ") | |
} | |
} |
후기
쉽게 풀 수 있는 문제였다.
반응형
'TIL > 코테 스터디' 카테고리의 다른 글
99클럽 코테 스터디 30일차 TIL: LIS (1) | 2024.11.26 |
---|---|
99클럽 코테 스터디 29일차 TIL: DP (0) | 2024.11.25 |
99클럽 코테 스터디 28일차 TIL: LIS 응용 (0) | 2024.11.24 |
99클럽 코테 스터디 27일차 TIL: LIS (0) | 2024.11.23 |
99클럽 코테 스터디 26일차 TIL: 게임 (0) | 2024.11.22 |