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 | 31 |
Tags
- domain data
- RxSwift
- custombottomsheet
- swift custom ui
- swift bottomsheet
- scene delegate
- SWIFT
- BFS
- swift 점선
- task cancellation
- UIKit
- 타임라인 포맷팅
- button configuration
- uikit toast
- swift concurrency
- rxdatasources
- traits
- coordinator
- DP
- swift dashed line
- custom ui
- reactorkit
- swift 백준
- custom navigation bar
- 버튼 피드백
- claen architecture
- Tuist
- task cancel
- identifiable
- swift navigationcontroller
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 16953 A -> B 본문

풀이 아이디어
- 두 가지 조건으로서 만들어지는 수를 찾아야 하는 문제라 오히려 거꾸로 찾아내는 방식을 택했습니다.
- A < B 가 보장되어있으므로, B가 짝수인경우와 , B의 끝자리가 1인 경우를 찾아 수를 점차 줄여나갑니다.
풀이
let ab = readLine()!.split(separator: " ").map{Int($0)!}
var (a,b) = (ab[0], ab[1])
func division2(_ input: Int) -> Int {
return input / 2
}
func subtract1(_ input: Int) -> Int {
var strInput = String(input)
strInput.removeLast()
return Int(strInput)!
}
// 최초 연산도 포함해야 합니다
// 출력초과를 방지하기 위해 false 카운트를 사용했습니다
var count = 1
var falseCount = 0
while a != b {
if b % 2 == 0 && b / 2 >= a {
b = division2(b)
count += 1
} else if String(b).last == "1" {
let temp = subtract1(b)
if temp < a {
falseCount += 1
break
}
b = temp
count += 1
} else {
falseCount += 1
break
}
}
// falseCount가 한번이라도 찍혔다면, 계산이 불가능하여 break 된 경우입니다.
falseCount >= 1 ? print(-1) : print(count)
'Algorithm' 카테고리의 다른 글
[Swift 백준] 1535 안녕 (0) | 2025.02.05 |
---|---|
[Swift 백준] 2193 이친수 (0) | 2025.02.01 |
[Swift 백준] 1676 팩토리얼 0의 개수 (0) | 2025.01.26 |
[Swift 백준] 음식물 피하기 (0) | 2025.01.24 |
[Swift 백준] 21921 블로그 (0) | 2025.01.23 |