๋ฐฑ์ค€ - ์ „์ž๋ ˆ์ธ์ง€(Swift)

2022. 10. 23. 17:25ใ†Algorithm

๋ฐฑ์ค€ - ์ „์ž๋ ˆ์ธ์ง€(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

10162๋ฒˆ: ์ „์ž๋ ˆ์ธ์ง€

3๊ฐœ์˜ ์‹œ๊ฐ„์กฐ์ ˆ์šฉ ๋ฒ„ํŠผ A B C๊ฐ€ ๋‹ฌ๋ฆฐ ์ „์ž๋ ˆ์ธ์ง€๊ฐ€ ์žˆ๋‹ค. ๊ฐ ๋ฒ„ํŠผ๋งˆ๋‹ค ์ผ์ •ํ•œ ์‹œ๊ฐ„์ด ์ง€์ •๋˜์–ด ์žˆ์–ด ํ•ด๋‹น ๋ฒ„ํŠผ์„ ํ•œ๋ฒˆ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ๊ทธ ์‹œ๊ฐ„์ด ๋™์ž‘์‹œ๊ฐ„์— ๋”ํ•ด์ง„๋‹ค. ๋ฒ„ํŠผ A, B, C์— ์ง€์ •๋œ ์‹œ๊ฐ„์€

www.acmicpc.net

 

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

 

ํฐ ๊ฒƒ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ์ด๋‹ค. 

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ๋กœ์ง์„ ํ•จ์ˆ˜๋กœ๋งŒ๋“ค์–ด์„œ print์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

๋‚˜๋„ ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•ด๋ด์•ผ๊ฒ ๋‹ค.

 

import Foundation

var n = Int(readLine()!)!
let arr: [Int] = [300, 60, 10]
var cnt: [Int] = [0, 0, 0]
var idx = 0
var flag = false

while n > 0 {
    if idx == 3 {
        flag = true
        break
    }
    if n >= arr[idx] {
        cnt[idx] = n / arr[idx]
        n = n % arr[idx]
    }
    idx += 1
}

if flag {
    print(-1)
} else {
    cnt.forEach{ print($0, terminator: " ") }   
}