๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Etc

[Swift&JS] HackerRank Staircase

by Fomagran ๐Ÿ’ป 2022. 2. 6.
728x90
๋ฐ˜์‘ํ˜•

 

Problem

 

 

Staircase | HackerRank

Print a right-aligned staircase with n steps.

www.hackerrank.com


Source Code

 

Swift

 

import Foundation

let n:Int = Int(readLine()!)!
var start = Array(repeating:" ", count: n)
func staircase(n: Int) {
    for i in 1...n {
        start[n-i] = "#"
        print(start.joined())
    }
}
staircase(n: n)

 

JS

 

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');
    main();
});

function readLine() {
    return inputString[currentLine++];
}

function staircase(n) {
    let start = " ".repeat(n).split('')
    for (let i = 1; i <= n; i++) {        
        start[n-i] = "#"
        console.log(start.join(''))
      } 
}

function main() {
    const n = parseInt(readLine().trim(), 10);
    staircase(n);
}
728x90
๋ฐ˜์‘ํ˜•

'๐Ÿ“– Problem Solution > Etc' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Swift] HackerRank Plus Minus  (0) 2022.02.02
[Swift] HackerRank Diagonal Difference  (0) 2022.02.01
[Swift] HackerRank A Very Big Sum  (0) 2022.01.31
[Swift] HackerRank Compare the Triplets  (0) 2022.01.31

๋Œ“๊ธ€