-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFive.java
More file actions
28 lines (23 loc) · 791 Bytes
/
Five.java
File metadata and controls
28 lines (23 loc) · 791 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
// Reverse the left and right numbers like
// You must use only a loop
// input 9
// output 4 3 2 1 5 9 8 7 6
// input 13
// output 6 5 4 3 2 1 7 13 12 11 10 9 8
import java.util.Scanner;
public class Five {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("input");
int n = scanner.nextInt();
scanner.close(); // Close scanner to avoid resource leaks
int mid = n / 2; // Calculate the middle point
for (int i = 1; i <= n; i++) {
if (i <= mid) {
System.out.print((mid - i + 1) + " "); // Print left side in reverse
} else {
System.out.print((i + 1) + " "); // Print right side normally
}
}
}
}