-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpiral_matrix2.java
60 lines (59 loc) · 1.3 KB
/
Spiral_matrix2.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
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
import java.util.*;
public class Spiral_matrix2
{
int size;
int m[][];
public Spiral_matrix2(int s)
{
m=new int[size=s][s];
}
public void Show()
{
for(int i=0;i<m.length;i++)
{
for(int j=0;j<m[i].length;j++)
{
System.out.printf("%4d",m[i][j]);
}
System.out.println();
}
}
public void fill()
{
int count=1;
int row1=0;
int col1=0;
int row2=m.length-1;
int col2=m.length-1;
while(count<=(m.length*m.length))
{
for(int i=col1;i<=col2;i++)
{
m[row1][i]=count++;
}
for(int i=row1+1;i<=row2;i++)
{
m[i][col2]=count++;
}
for(int i=col2-1;i>=col1;i--)
{
m[row2][i]=count++;
}
for(int i=row2-1;i>=row1+1;i--)
{
m[i][row1]=count++;
}
row1++;row2--;
col1++;col2--;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows and column=");
int size=sc.nextInt();
Spiral_matrix2 obj=new Spiral_matrix2(size);
obj.fill();
obj.Show();
}
}