Swift Data Structure And Algorithm/Basic Number Theory
combinationpascal
youngjaeLee1026
2022. 3. 8. 21:59
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()
μ 체μ½λλ μ¬κΈ°μμ νμΈν μ μμ΅λλ€.