-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspiral_display.java
More file actions
61 lines (47 loc) · 1.1 KB
/
spiral_display.java
File metadata and controls
61 lines (47 loc) · 1.1 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.*;
import java.util.*;
public class spiral_display{
public static void main(String args[]) throws Exception{
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int m = scn.nextInt();
int[][] arr = new int[n][m];
for(int i = 0;i<arr.length;i++){
for(int j = 0; j < a[0].length; j++){
arr[i][j] = scn.nextInt();
}
}
int minr = 0;
int minc = 0;
int maxr = arr.length - 1;
int maxc = arr[0].length - 1;
int count = 0;
int tne = n * m;
while(count < tne){
// left wall
for(int i = minr, j = minc; i <= maxr && count < tne; i++){
System.out.println(arr[i][j]);
count++;
}
minc++;
// bottom wall
for(int i = maxr, j = minc; j <= maxc && count < tne; j++){
System.out.println(arr[i][j]);
count++;
}
maxr--;
// right wall
for(int i = maxr, j=maxc; i >= minr && count < tne; i--){
System.out.println(arr[i][j]);
count++;
}
maxc--;
// top wall
for(int i = minr, j = maxc; j >= minc && count < tne; j--){
System.out.println(arr[i][j]);
count++;
}
minr++;
}
}
}