알고리즘

[백준 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

 

2522번: 별 찍기 - 12

첫째 줄부터 2×N-1번째 줄까지 차례대로 별을 출력한다.

www.acmicpc.net