-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUshapedLayersMatrix.java
33 lines (31 loc) · 1.11 KB
/
UshapedLayersMatrix.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
27
28
29
30
31
32
33
import java.util.Scanner;
public class UshapedLayersMatrix {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int R = scanner.nextInt();
int C = scanner.nextInt();
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
matrix[i][j] = scanner.nextInt();
}
}
printULayers(matrix, R, C);
}
private static void printULayers(int[][] matrix, int R, int C) {
int top = 0, bottom = R - 1, left = 0, right = C - 1;
while (left < right && bottom >= 0) {
for (int i = top; i <= bottom; i++) {
System.out.print(matrix[i][left] + " ");
}
for (int i = left + 1; i <= right; i++) {
System.out.print(matrix[bottom][i] + " ");
}
for (int i = bottom - 1; i > top; i--) {
System.out.print(matrix[i][right] + " ");
}
System.out.println();
bottom--; left++; right++;
}
}
}