-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarathonWaveChart.java
More file actions
46 lines (33 loc) · 1.33 KB
/
MarathonWaveChart.java
File metadata and controls
46 lines (33 loc) · 1.33 KB
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
44
45
46
import java.util.Scanner;
public class MarathonWaveChart {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int totalPrinted = 0;
if (n > 10) {
System.out.println("Abhik’s marathon journey intensifies! Let’s see his progress chart:");
}
int maxWidth = (n - 1) * 4 + 1;
for (int day = 1; day <= n; day++) {
StringBuilder line = new StringBuilder();
if (day % 2 == 1) {
for (int i = 1; i <= day; i++) {
line.append(i);
totalPrinted++;
if (i != day) line.append(" ");
}
} else {
for (int i = day; i >= 1; i--) {
line.append(i);
totalPrinted++;
if (i != 1) line.append(" ");
}
}
int lineLength = line.length();
int leftPadding = (maxWidth - lineLength) / 2;
System.out.printf("%" + (leftPadding + lineLength) + "s%n", line.toString());
}
System.out.println("Total numbers printed: " + totalPrinted);
sc.close();
}
}