Created
July 10, 2017 09:17
-
-
Save hcn1519/e617510190f862dc1563890e66807c42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// Pascal Triangle | |
// | |
// Created by woowabrothers on 2017. 7. 10.. | |
// Copyright © 2017년 woowabrothers. All rights reserved. | |
// | |
import Foundation | |
func makePascalTriangle(numOfRows : Int) -> ([[Int]]) { | |
var result: [[Int]] = [] | |
// input이 1 | |
result.append([1]) | |
if numOfRows == 1 { | |
return result | |
} | |
// input이 2 | |
result.append([1, 1]) | |
if numOfRows == 2 { | |
return result | |
} | |
// input이 그 이상일 경우 | |
var prev = [1, 1] | |
for _ in 1...numOfRows { | |
var temp = [1] | |
for i in prev.indices { | |
if i != prev.count - 1 { | |
temp.append(prev[i] + prev[i+1]) | |
} | |
} | |
temp.append(1) | |
result.append(temp) | |
prev = temp | |
} | |
return result | |
} | |
// run할 때 argument 받기 | |
for arg in CommandLine.arguments { | |
if let num = Int(arg) { | |
let resultValue = makePascalTriangle(numOfRows: num) | |
for value in resultValue { | |
print(value) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment