Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Vytvoril som riešenie v týchto programovacích jazykoch:
Všetky riešenia sú dostupné aj na mojom GitHub profile.
Scala
1 2 3 4 5 6 7 8 9 |
object Staircase extends App { val n = io.Source.stdin.bufferedReader().readLine().toInt Range(1, n + 1).foreach { i => println(stair(i, n)) } def stair(i: Int, n: Int) = " " * (n - i) + "#" * i } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.*; public class Staircase { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int height = Integer.parseInt(stdin.nextLine()); for(int i = 0; i < height; i++) { for(int j = 0; j < height; j++) { if(height - i - 2 < j) { System.out.print("#"); } else { System.out.print(" "); } } System.out.println(); } stdin.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
function processData(input) { var n = parseInt(input); for(var i = 0; i < n; i++) { var s = ""; for(var j = 0; j < n; j++) { if(n - i - 2 < j) { s += "#"; } else { s += " "; } } console.log(s); } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Ruby
1 2 3 4 5 6 7 8 9 |
def stair(spaces, hashes) " " * spaces + "#" * hashes end n = gets.to_i for i in 1..n do puts stair(n - i, i) end |