728x90
반응형
https://www.acmicpc.net/problem/11047
- 그리디 알고리즘
import Foundation
let input1 = readLine()!.split(separator: " ").map{Int(String($0))!}
let kindsOfCoin = input1[0]
let valueOfCoin = input1[1]
var coins:[Int] = []
for _ in 0 ..< kindsOfCoin {
coins.append(Int(readLine()!)!)
}
var value = valueOfCoin
var countOfCoin = 0
var prevCoinIdx = kindsOfCoin - 1
while value > 0 {
var i = prevCoinIdx
while i >= 0 && value < coins[i] {
i -= 1
}
prevCoinIdx = i
let countCurrent = (value - (value % coins[i])) / coins[i]
countOfCoin += countCurrent
value -= (countCurrent * coins[i] )
}
print(countOfCoin)
728x90
반응형
'개발 > 코딩' 카테고리의 다른 글
백준 - 보물 - Swift (0) | 2022.07.10 |
---|---|
백준 - 회의실 배정 - Swift (0) | 2022.07.10 |
백준 - ATM - Swift (0) | 2022.07.10 |
백준 - 설탕 배달 - Swift (0) | 2022.07.10 |
해커랭크(HackerRank) - Almost Sorted // C++ (0) | 2021.05.19 |
댓글