Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 11057๋ฒˆ - ์˜ค๋ฅด๋ง‰ ์ˆ˜ (dp, 3์ค‘ for๋ฌธ ์‚ฌ์šฉ)

๊ฐ์ž ๐Ÿฅ” 2022. 3. 25. 00:09
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๋งํฌ

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

 

11057๋ฒˆ: ์˜ค๋ฅด๋ง‰ ์ˆ˜

์˜ค๋ฅด๋ง‰ ์ˆ˜๋Š” ์ˆ˜์˜ ์ž๋ฆฌ๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์„ ์ด๋ฃจ๋Š” ์ˆ˜๋ฅผ ๋งํ•œ๋‹ค. ์ด๋•Œ, ์ธ์ ‘ํ•œ ์ˆ˜๊ฐ€ ๊ฐ™์•„๋„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์นœ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 2234์™€ 3678, 11119๋Š” ์˜ค๋ฅด๋ง‰ ์ˆ˜์ด์ง€๋งŒ, 2232, 3676, 91111์€ ์˜ค๋ฅด๋ง‰ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ˆ˜

www.acmicpc.net

 

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด - ๋งž์•˜์Šต๋‹ˆ๋‹ค!

let n = Int(readLine()!)!
var dp = Array(repeating: Array(repeating: 0, count: 10), count: 1001)

//sum ํ•จ์ˆ˜ ๊ตฌํ˜„
func sum(_ numbers: [Int]) -> Int {
    return numbers.reduce(0, +)
}

// ํ•œ์ž๋ฆฌ์ˆ˜๋Š” ๋ฌด์กฐ๊ฑด 1๊ฐœ์”ฉ
for i in 0...9 {
    dp[1][i] = 1
}

if n ==  1 {
    print(sum(dp[1]))
} else {
    for i in 2..<n+1 {
        for j in 0...9 {
            for k in j...9 {
                dp[i][j] = (dp[i][j] + dp[i-1][k]) % 10007
            }
        }
    }
    print(sum(dp[n])%10007)
}
๋ฐ˜์‘ํ˜•