Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- identifiable
- traits
- domain data
- custom navigation bar
- task cancel
- swift custom ui
- SWIFT
- swift concurrency
- swift dashed line
- uikit toast
- swift 백준
- Tuist
- custom ui
- button configuration
- UIKit
- RxSwift
- swift 점선
- task cancellation
- claen architecture
- paragraph style
- custombottomsheet
- swift bottomsheet
- reactorkit
- BFS
- coordinator
- DP
- tusit font 추가 방법
- 타임라인 포맷팅
- swift navigationcontroller
- rxdatasources
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 10610 30 본문
풀이 아이디어
그리디 알고리즘 문제입니다.
조건을 떠올리면 바로 풀 수 있는 문제인데
30의 배수라는 조건을 떠올려보면,
일단 끝이 무조건 0입니다.
또 각 자릿수의 합이 무조건 3의 배수입니다.
이 조건을 따른 자세한 풀이는 아래와 같습니다.
풀이
let n = readLine()!
let arr = n.map { Int(String($0))! }
// 0을 가지고있고, 배열 총합이 3의 배수인지 확인합니다.
if arr.contains(0) && arr.reduce(0, +) % 3 == 0 {
//숫자를 뺄수있는 문제가 아니므로 그냥 무조건 정답입니다.
//다른 순서대로 섞어도 결국 각자리 합이 3의 배수가 안나오면 30의 배수가 될 수 없음
let result = arr.sorted(by: >).map { String($0) }.joined()
print(result)
} else {
print(-1)
}
'Algorithm' 카테고리의 다른 글
[Swift 백준] 2212 센서 (0) | 2025.04.15 |
---|---|
[Swift 백준] 1269 대칭 차집합 (0) | 2025.04.10 |
[Swift 백준] 11052 카드 구매하기 (0) | 2025.04.03 |
[Swift 백준] 1475 방 번호 (0) | 2025.03.31 |
[Swift 백준] 9465 스티커 (0) | 2025.03.28 |