알고리즘 단련장/해커랭크

[해커랭크] Staircase

snapcoder 2025. 7. 27. 19:34
728x90
반응형
SMALL

https://www.hackerrank.com/challenges/staircase/problem?isFullScreen=true

 

Staircase | HackerRank

Print a right-aligned staircase with n steps.

www.hackerrank.com

 

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  function with the following parameter(s):

  • : an integer

Print

Print a staircase as described above. No value should be returned.
Note: The last line is not preceded by spaces. All lines are right-aligned.

Input Format

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

Constraints

 .

Sample Input

6 

Sample Output

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

Explanation

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

 

 

 

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'staircase' function below.
     *
     * The function accepts INTEGER n as parameter.
     */

    public static void staircase(int n) throws IOException {
    // Write your code here
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        for(int line=1; line<=n; line++){
            for(int index=1; index<=n; index++){
                // 5, 1
                // 4, 2
                // 3, 3
                if (index <= (n-line)){
                    bw.write(" ");
                }
                else{
                    bw.write("#");
                }
            }
            bw.newLine();
        }
        bw.close();

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        Result.staircase(n);

        bufferedReader.close();
    }
}

 

728x90
반응형
LIST

'알고리즘 단련장 > 해커랭크' 카테고리의 다른 글

[해커랭크] Birthday Cake Candles  (0) 2025.08.06
[해커랭크] Mini-Max Sum  (0) 2025.08.06
[해커랭크] Plus Minus  (1) 2025.07.27
[해커랭크] Diagonal Difference  (1) 2025.07.27
[해커랭크] A Very Big Sum  (0) 2025.07.27