๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ๋™์ „ 0 BOJ #11047

by seolhee2750 2021. 8. 13.
๋ฌธ์ œ ์„ค๋ช…

์ค€๊ทœ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋™์ „์€ ์ด N์ข…๋ฅ˜์ด๊ณ , ๊ฐ๊ฐ์˜ ๋™์ „์„ ๋งค์šฐ ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

๋™์ „์„ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•ด์„œ ๊ทธ ๊ฐ€์น˜์˜ ํ•ฉ์„ K๋กœ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ ํ•„์š”ํ•œ ๋™์ „ ๊ฐœ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 10, 1 ≤ K ≤ 100,000,000)

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๋™์ „์˜ ๊ฐ€์น˜ Ai๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ai ≤ 1,000,000, A1 = 1, i ≥ 2์ธ ๊ฒฝ์šฐ์— Ai๋Š” Ai-1์˜ ๋ฐฐ์ˆ˜)

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— K์›์„ ๋งŒ๋“œ๋Š”๋ฐ ํ•„์š”ํ•œ ๋™์ „ ๊ฐœ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ

10 4200
1
5
10
50
100
500
1000
5000
10000
50000

์ถœ๋ ฅ

6

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import Foundation

let info = readLine()!.split(separator: " ").map({Int(String($0))!})
var money = [Int]()
var left = info[1]
var answer = 0

for _ in 0..<info[0] {
    let input = Int(readLine()!)!
    money.append(input)
}

while left > 0 {
    answer += left / money.last!
    left = left % money.removeLast()
}

print(answer)

์ด ๋ฌธ์ œ๋Š” ๊ทธ๋ฆฌ๋””!

  • ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ž…๋ ฅ๋˜๋Š” ๋ˆ์˜ ๋‹จ์œ„๋ฅผ money ๋ฐฐ์—ด์— ์ฐจ๋ก€๋กœ ์ €์žฅํ–ˆ๋‹ค. (for๋ฌธ)
  • info[1]๋กœ ์ž…๋ ฅ๋œ ๋ˆ ์•ก์ˆ˜๋ฅผ left ๋ณ€์ˆ˜์— ๋‹ด์•„์ฃผ์—ˆ๋‹ค. ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๋‚จ์€ ์•ก์ˆ˜๋ฅผ ๋‹ด๋Š” ๋ณ€์ˆ˜์ด๋‹ค.
  • while ๋ฌธ์—์„œ๋Š” left ๋ณ€์ˆ˜์— ๋‚จ์€ ๋ˆ์ด 0๋ณด๋‹ค ํด ๋•Œ ๋ฐ˜๋ณตํ•˜๊ฒŒ ํ•ด์คฌ๊ณ ,
    ๊ฒฐ๊ณผ๋ฅผ ๋‹ด๋Š” answer์—, ๋‚จ์€ ๋ˆ left๋ฅผ money ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ์•ก์ˆ˜(๋งˆ์ง€๋ง‰ ์š”์†Œ)๋กœ ๋‚˜๋ˆˆ ๊ฒฐ๊ณผ๋ฅผ ๋”ํ–ˆ๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ๋‚จ์€ ๋ˆ์„ ์—…๋ฐ์ดํŠธํ–ˆ๋Š”๋ฐ, left๋ฅผ money ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ์•ก์ˆ˜๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ €์žฅํ–ˆ๋‹ค.
    removeLast๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋งˆ๋‹ค ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•  ํ•„์š” ์—†์ด ๋งˆ์ง€๋ง‰ ์š”์†Œ๋กœ ๊ณ„์‚ฐํ•˜๋ฉด ๋˜๋„๋กํ•˜์—ฌ ํšจ์œจ์„ฑ์„ ๋†’์˜€๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์•„๋‹ˆ๋ผ ๊ธˆ๋ฐฉ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ์ตœ์ ์˜ ํƒ์ƒ‰ ๋ฐฉ๋ฒ•์„ ๊ธˆ๋ฐฉ ์ฐพ์„ ์ค„ ์•„๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ๋“ฏํ•˜๋‹ค.

 


 

๋ฌธ์ œ

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

๋Œ“๊ธ€