youngjaeLee1026 2022. 3. 8. 21:59

1. 문제

스크란샷 2022-03-08 21 55 49

2. μž…μΆœλ ₯

스크란샷 2022-03-08 21 56 09

3. μž…μΆœλ ₯ μ˜ˆμ‹œ

스크란샷 2022-03-08 21 56 45

4. 문제 섀계

  1. 2차원 배열을 κ΅¬μ„±ν•˜μ—¬ μ™Όμͺ½ μœ„μ™€ λ°”λ‘œ μœ„μ˜ 값을 λ”ν•˜μ—¬ μžμ‹ μ„ κ²°μ •ν•˜λŠ” ν˜•νƒœμΈ 파슀칼 μ‚Όκ°ν˜•μ„ ꡬ성
  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()

 

μ „μ²΄μ½”λ“œλŠ” μ—¬κΈ°μ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.