1. ๋ฌธ์
2. ์ ์ถ๋ ฅ
3. ์ ์ถ๋ ฅ ์์
4. ๋ฌธ์ ์ค๊ณ
- 2์ฐจ์ ๋ฐฐ์ด์ ๊ตฌ์ฑํ์ฌ ์ผ์ชฝ ์์ ๋ฐ๋ก ์์ ๊ฐ์ ๋ํ์ฌ ์์ ์ ๊ฒฐ์ ํ๋ ํํ์ธ ํ์ค์นผ ์ผ๊ฐํ์ ๊ตฌ์ฑ
- ํ์ค์นผ ์ผ๊ฐํ์ ์ ์ฅ๋์ด ์๋ ๊ฐ์ ๊ฐ ํ์ด n C m์ ๋ปํ๋ฏ๋ก ํด๋น ์์ ๊ฐ์ ์ถ๋ ฅํจ์ผ๋ก์จ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํจ
5. ์ ์ฒด ์ฝ๋
//
// main.swift
// CombinationPascal
//
// Created by ์ด์์ฌ on 2022/03/08.
//MARK: - combinationpascal
//MARK: - Framework
import Foundation
//MARK: - Function
func solution() -> Void {
//MARK: - Input
guard let input = readLine()?.components(separatedBy: " ") else { return }
let n: Int = input.map { Int($0) }[0] ?? 0
let m: Int = input.map { Int($0) }[1] ?? 0
var combinationPascal: [[Int]] = Array(repeating: Array(repeating: 0, count: n + 10), count: n + 10)
//MARK: - Process
combinationPascal[0][0] = 1
for i in 1...n {
combinationPascal[i][0] = 1 // n C 0 = 1
combinationPascal[i][i] = 1 // n C n = 1
for j in 1..<i {
combinationPascal[i][j] = combinationPascal[i - 1][j - 1] + combinationPascal[i - 1][j]
}
}
//MARK: - Output
print(combinationPascal[n][m])
}
solution()
์ ์ฒด์ฝ๋๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.
'Swift Data Structure And Algorithm > Basic Number Theory' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
streetree (0) | 2022.03.08 |
---|---|
combinationzero (0) | 2022.03.08 |
beehive (0) | 2022.03.08 |
findprime (0) | 2022.03.08 |
fractionsum (0) | 2022.03.08 |