Reputation: 55
This solution to the staircase prints the correct output, but in the opposite direction. Any idea how to alter this solution to get the desired result?
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the staircase function below.
static void staircase(int n) {
int counter=0;
for(int i=0; i<n;i++)
{
//for (int k=0; k<n-k-1;k++)
// {
// System.out.println("");
// }
System.out.print("\n");
System.out.print("#");
counter++;
for(int j=0; j<counter-1;j++)
{
System.out.print("#");
}
}
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
staircase(n);
scanner.close();
}
}
Input:
6
Expected output:
#
##
###
####
#####
######
Actual Output:
#
##
###
####
#####
######
It's for a hackerrank easy challenge. I just want to be able to use my approach that I discovered instead of a solution I find online. Thanks!
Upvotes: 0
Views: 5689
Reputation: 657
public static void staircase(int n) {
int counter = 0;
for (int i = 1; i <= n; i++) {
counter++;
for (int x = n-1; x >= i; x--) {
System.out.print(" ");
}
for (int j = 0; j <= counter - 1; j++) {
System.out.print("#");
}
if (i != n) {
System.out.println();
} else {
System.out.print("");
}
}
}
Upvotes: 0
Reputation: 1
And another way
fun staircase(n: Int): Unit {
for (i in 1..n) {
print(" ".repeat(n-i))
println("#".repeat(i))
}
}
Upvotes: 0
Reputation: 17655
Very Simple and Short way to do this-
for (int i = 1; i <= n; i++)
{
System.out.println(new String(new char[n-i]).replace("\0", " ") + new String(new char[i]).replace("\0", "#"));
}
Upvotes: 0
Reputation: 15
This should work
static void staircase(int n) {
String p = "";
for(int i = 0 ; i< n ; i++){
p = p + "#";
System.out.printf("%"+n+"s\n", p);
}
}
Upvotes: 0
Reputation: 448
An other proposition :
static void staircase(int n) {
for(int i=n; i> 0; i--) {
String s= "";
for(int j=1; j<=n-i+1; j++) s+="#";
System.out.printf("%"+n+"s\n", s);
}
}
Upvotes: 0
Reputation: 12819
You simply have to make two changes:
First you have to add a loop that will print spaces. If you start the loop at n
and loop until more than or equal to i
, while subtracting, this will print the correct amount of spaces.
Second, you need to have the newline at the end of the loop:
static void staircase(int n) {
int counter=0;
for(int i=0; i<n;i++)
{
counter++;
for(int k=n; k>= i;k--)
{
System.out.print(" ");
}
for(int j=0; j<=counter-1;j++)
{
System.out.print("#");
}
System.out.print("\n");
}
}
Output:
#
##
###
####
#####
######
Also just a note on your code. It is bad practice to close System.in
. The general rule is that if you did not open a resource, you should not close it.
Upvotes: 2
Reputation: 78
No need to use extra counter
variable. Just try this:
static void printStaircase(int n) {
if (n < 0) {
return;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
System.out.print('\u00a0'); // nbsp
}
for (int j = n - i + 1; j <= n; ++j) {
System.out.print('#');
}
System.out.println();
}
}
Upvotes: 1