๋ฐฑ์ค€ - ๊ฑฐ์Šค๋ฆ„๋ˆ(Swift)

2022. 10. 23. 16:28ใ†Algorithm

๋ฐฑ์ค€ - ๊ฑฐ์Šค๋ฆ„๋ˆ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://www.acmicpc.net/problem/5585

 

5585๋ฒˆ: ๊ฑฐ์Šค๋ฆ„๋ˆ

ํƒ€๋กœ๋Š” ์ž์ฃผ JOI์žกํ™”์ ์—์„œ ๋ฌผ๊ฑด์„ ์‚ฐ๋‹ค. JOI์žกํ™”์ ์—๋Š” ์ž”๋ˆ์œผ๋กœ 500์—”, 100์—”, 50์—”, 10์—”, 5์—”, 1์—”์ด ์ถฉ๋ถ„ํžˆ ์žˆ๊ณ , ์–ธ์ œ๋‚˜ ๊ฑฐ์Šค๋ฆ„๋ˆ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ๊ฒŒ ์ž”๋ˆ์„ ์ค€๋‹ค. ํƒ€๋กœ๊ฐ€ JOI์žกํ™”์ ์—์„œ ๋ฌผ๊ฑด์„ ์‚ฌ

www.acmicpc.net

 

 ๋‚˜์˜ ํ’€์ด

 

ํฐ ์ˆ˜๋ถ€ํ„ฐ ๊ฑฐ์Šค๋ฆ„๋ˆ์ด ์ฑ„์›Œ์งˆ๋•Œ๊นŒ์ง€ ๋‚˜๋ˆ„๋ฉด ๋˜๋Š” ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ์ด๋‹ค. 

let arr = [500, 100, 50, 10, 5, 1]

var n = Int(readLine()!)!
var money = 1000 - n
var cnt = 0
var idx = 0
while money > 0 {
    if money >= arr[idx] {
        cnt += money / arr[idx]
        money = money % arr[idx]
    }
    idx += 1
}

print(cnt)