본문 바로가기

알고리즘

해커랭크 Staircase

Staircase detail

This is a staircase of size :

# ## ### ####

Its base and height are both equal to . It is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size .

Function Description

Complete the staircase function in the editor below.

staircase has the following parameter(s):

  • int n: an integer

Print

Print a staircase as described above.

Input Format

A single integer, , denoting the size of the staircase.

Constraints

 .

Output Format

Print a staircase of size  using # symbols and spaces.

Note: The last line must have  spaces in it.

Sample Input

6

Sample Output

# ## ### #### ##### ######

Explanation

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of .

import Foundation

// Complete the staircase function below.
func staircase(n: Int) -> Void {
    var str: String = ""

    for i in (1...n).reversed() {
        str += String(repeating: " ", count: i - 1) + String(repeating: "#", count: n + 1 - i) + "\n"  
    } 
	print("\(str)")
}

guard let n = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }

staircase(n: n)

'알고리즘' 카테고리의 다른 글

[LeetCode] Two Sum  (0) 2021.08.31
해커랭크 Diagonal Difference 문제풀이  (0) 2020.09.05
해커랭크 Compare the Triplets 문제풀이  (0) 2020.09.04