forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaircase.java
More file actions
43 lines (38 loc) ยท 935 Bytes
/
Staircase.java
File metadata and controls
43 lines (38 loc) ยท 935 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
package hackerrank;
public class Staircase {
static void staircase(int n) {
//sol1
for (int i = 1; i <= n; i++) {
System.out.print(new String(new char[n - i]).replace("\0", " "));
System.out.println(new String(new char[i]).replace("\0", "#"));
}
//sol2
/*
System.out.println("---sol2");
int star = 1, space = n - 1;
while (n > 0) {
for (int i = 0; i < space; i++) {
System.out.print(" ");
}
for (int i = 0; i < star; i++) {
System.out.print("#");
}
star = star + 1;
space = space - 1;
--n;
System.out.println("");
}
*/
//sol3
System.out.println("---sol3");
StringBuilder sb = new StringBuilder(n);
String fmt = "%" + n + "s\n";
for (int i = 1; i <= n; i++) {
sb.append('#');
System.out.printf(fmt, sb);
}
}
public static void main(String[] args) {
staircase(6);
}
}