Spiral Matrix algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.