알고리즘
[백준 2522][Swift] 별 찍기 - 12
moving
2022. 4. 1. 18:28
728x90
코드
let input = Int(readLine()!)!
for i in 1...(2 * input - 1) {
var total: String = ""
if i <= input {
for j in 1...input {
if j <= input - i {
total += " "
} else {
total += "*"
}
}
} else {
for j in 1...input {
if j <= i - input {
total += " "
} else {
total += "*"
}
}
}
print(total)
}
혹은
let input = Int(readLine()!)!
for i in 1 ... (2 * input - 1) {
if i <= input {
print(String(repeating: " ", count: input - i) + String(repeating: "*", count: i))
} else {
print(String(repeating: " ", count: i - input) + String(repeating: "*", count: 2 * input - i))
}
}
https://www.acmicpc.net/problem/2522