I. Requirements:
determinant, line and width are not necessarily equal, require a clockwise spiral to print each element, the so-called clockwise spiral is:
First lap: Start with the first column element in the first row, print the first row of all elements from left to right, then print the last remaining column element, print the remaining elements from right to left, and then print the remaining elements of the first column from bottom to top
Second circle: Prints a circle of elements in the clockwise order of the second row, starting at the second column
..., loop over and over until all the elements have been printed out without duplication.
The counter-clockwise spiral is exactly the opposite of the above order.
Two. Analysis:
Clockwise spiral printing can divide the problem into: first press clockwise to print the first lap, and then press clockwise to print the second lap, where each lap is divided into four steps: from left to right, from top to bottom, right to left, bottom to top.
Anti-clockwise spiral printing can also divide the problem into the same: first press the first circle counterclockwise, and then press the clockwise to print the second lap, where each lap is divided into four steps: from top to bottom, from left to right, from bottom to top, right to left.
Three. Code:
public class luoxunarr{
public static void Main (String [] args) {
Luoxunarr Larr =new Luoxunarr ();
int a[][]= {{0,1,2,3,4,5,6,7},{27,28,29,30,31,32,33,8},{26,47,48,49,50,51,34,9},{25,46,59,60,61,52,35,10},{ 24,45,58,63,62,53,36,11},{23,44,57,56,55,54,37,12},{22,43,42,41,40,39,38,13},{21,20,19,18,17,16,15,14}};
int [][]a=larr.inttwoarr (11,9);
Larr.listinttwoarr (a);//print an array in normal order
Larr.luotwoarr (a);//print an array by clockwise spiral
Larr. Reluotwoarr (a);//print an array in a counterclockwise spiral
}
Used to generate random integers
public int randomint (int min,int max) {
Math.random () [0,1)
return (int) (Math.random () * (max+1-min) +min);
}
Used to generate two-dimensional arrays and assign values to each element with random integers
Public int[][] Inttwoarr (int line,int col) {
Int[][] a= new int[line][];
for (int i=0;i<line;i++) {
A[i] =new Int[col];
}
for (int i=0;i<line;i++) {
for (int j=0;j<col;j++) {
A[i][j]=randomint (0,9);
}
}
return A;
}
Used to print two-dimensional arrays in normal order
public void Listinttwoarr (int [] [] a) {
for (int i=0;i<a.length;i++) {
for (int j=0;j<a[i].length;j++) {
System.out.printf ("%3d", A[i][j]);
}
System.out.println ();
}
System.out.println ();
}
Used to print two-dimensional arrays in a clockwise spiral
public void Luotwoarr (int[][] a) {
int len = a.length; Number of rows in the determinant
int col = number of columns of the a[0].length;//determinant
int mid=len<col?len:col;//The minimum value of rows and columns
int hang=0;//to record the number of lines that have been printed
int lie=0;//to record the number of columns that have been printed
int flag1=0;//used to record row or column labels
int flag2=0;//used to record row or column labels
int i=0;
int sum=0;
for (int start=0;start<=mid/2;start++) {
1. Print lines left to right
if (Hang<len) {
for (i=start;i<col-start;i++) {
System.out.print (A[start][i] + "");
sum++;
}
Flag1=i-1;
hang++;//number of lines printed plus 1
}
2. Print columns from top to bottom
if (Lie<col) {
for (i=start+1;i<len-start;i++) {
System.out.print (A[i][flag1] + "");
sum++;
}
Flag2=i-1;
lie++; Number of printed columns plus 1
}
3. Print rows from right to left
if (Hang<len) {
for (i=flag1-1;i>=start;i--) {
System.out.print (A[flag2][i] + "");
sum++;
}
Flag1=i;
hang++;//number of lines printed plus 1
}
4. Print columns from bottom to top
if (Lie<col) {
for (i=flag2-1;i>start;i--) {
System.out.print (A[i][start] + "");
sum++;
}
lie++; Number of printed columns plus 1
}
System.out.println ("Altogether printed" +sum+ "elements");
}
}
Used to print two-dimensional arrays in a counterclockwise spiral
public void Reluotwoarr (int[][] a) {
int len = a.length; Number of rows in the determinant
int col = number of columns of the a[0].length;//determinant
int mid=len<col?len:col;//The minimum value of rows and columns
int hang=0;//to record the number of lines that have been printed
int lie=0;//to record the number of columns that have been printed
int flag1=0;//used to record row or column labels
int flag2=0;//used to record row or column labels
int i=0;
int sum=0;
for (int start=0;start<=mid/2;start++) {
1. Top to bottom print column
if (Lie<col) {
for (i=start;i<len-start;i++) {
System.out.print (A[i][start] + "");
sum++;
}
Flag1=i-1;
lie++;//number of lines printed plus 1
}
2. Print lines from left to right
if (Hang<len) {
for (i=start+1;i<col-start;i++) {
System.out.print (A[flag1][i] + "");
sum++;
}
Flag2=i-1;
hang++; Number of printed columns plus 1
}
3. Print columns from bottom to top
if (Lie<col) {
for (i=flag1-1;i>=start;i--) {
System.out.print (A[i][flag2] + "");
sum++;
}
Flag1=i;
lie++;//number of lines printed plus 1
}
4. Print rows from right to left
if (Hang<len) {
for (i=flag2-1;i>start;i--) {
System.out.print (A[start][i] + "");
sum++;
}
hang++; Number of printed columns plus 1
}
System.out.println ("Altogether printed" +sum+ "elements");
}
}
}
This article is from the "Httpyuntianjxxll.spac.." Blog, make sure to keep this source http://333234.blog.51cto.com/323234/1784255
Clockwise and counterclockwise spiral print two-dimensional array (determinant)