Spiral Output 1-25
public class Sequence {
public static void Main (string[] args) {
int n = 5;
0: Right, 1: down, 2: Left, 3: Up
int direction = 0;
Row, column
int row = 0, col = 0;
int num = 0;
int[] Array = new INT[N * n];
while (Array[row * n + col] = = 0) {
num++;
Array[row * n + col] = num;
switch (direction) {
Case 0:
col++;
Break
Case 1:
row++;
Break
Case 2:
col--;
Break
Case 3:
row--;
Break
}
if (row = = N | | col = = N | | row = = 1 | | col = =-1
|| Array[row * n + col]! = 0) {
direction++;
if (Direction = = 4)
Direction = 0;
switch (direction) {
Case 0:
row++;
col++;
Break
Case 1:
row++;
col--;
Break
Case 2:
row--;
col--;
Break
Case 3:
row--;
col++;
Break
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; J < N; j + +) {
System.out.printf ("%-3s", Array[i * n + j]);
}
System.out.println ();
}
}
}
-----------------------------------------------------------
Matrix Conversion
public class Testarray {
public static void Main (string[] args) {
int array[][] = {{22, 18, 36}, {27, 34, 58}, {12, 51, 32},
{14, 52, 64}};Create a two-dimensional array of 4 rows and 3 columns
int brray[][] = new INT[3][4];Create an array of 3 rows and 4 columns to receive the matrix after the transpose
SYSTEM.OUT.PRINTLN ("Prototype matrix for example the following:");
for (int i = 0; i < Array.Length; i++) {Iterating through an element in an array
for (int j = 0; J < Array[i].length; J + +) {
System.out.print (Array[i][j] + "");
}
System.out.println ();
}
for (int i = 0; i < Array.Length; i++) {At this point I is the row of array arrays, the columns of Brray
for (int j = 0; J < Brray.length; J + +) {The J at this point is the column of array arrays, Brray rows
Brray[j][i] = Array[i][j];Assigns the elements of line I j in array array to the J row I column in the Brray array
}
}
System.out.println ("\ n transpose the matrix after the following example:");
for (int i = 0; i < brray.length; i++) {Traversing the elements in the Brray array after transpose
for (int j = 0; J < Brray[i].length; J + +) {
System.out.print (Brray[i][j] + "");
}
System.out.println ();
}
}
}
Spiral Matrix algorithm