Last active
January 23, 2017 15:52
-
-
Save 5SMNOONMS5/9d562c93a48b4b1c2e753c3967ee4761 to your computer and use it in GitHub Desktop.
Print right-angled-triangle or isosceles-triangle
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
// mode: 0 是 直角三角形, 1 是 等腰三角形 | |
// size: 邊長 | |
func printer(mode: Int, size: Int) { | |
// Print a right angled triangle | |
func rightAngledTriangle() { | |
var i = 1 | |
while i <= size { | |
var string = "" | |
for _ in 0..<i { string += "*" } | |
print(string + "\n") | |
i += 1 | |
} | |
} | |
// Print a isosceles triangle | |
func isoscelesTriangle() { | |
var i = size - 1 | |
while i >= 0 { | |
var string = "" | |
for _ in 0..<i { string += " " } | |
let count = (size - i - 1) * 2 + 1 | |
for _ in 0..<count { string += "*" } | |
print(string + "\n") | |
i -= 1 | |
} | |
} | |
(mode == 0) ? rightAngledTriangle() : isoscelesTriangle() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment