Swift Data Structure And Algorithm/Basic Number Theory

combinationpascal

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()

 

์ „์ฒด์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

'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