Just upload the code.
The questions encountered in Tencent’s online test last night.
A spiral matrix refers to a spiral matrix. Its numbers continue to grow from the first line to the right, from the first line to the right, from the downwards, from the left, and from the upwards, and cycle in this way.
import java.util.Scanner;public class mysnakematrix {private int n;//private int a[][];// Declare a matrix private int value = 1;// The value of the number in the matrix public mysnakematrix(int i) {this.n = i;a = new int[n][n];}// Calculate the number in the upper left corner of the m-th layer private int getcorner(int m) {int corner = 1;int o = n - 1;for (int i = 0; i < m - 1; ++i) {corner += 4 * o;o = o - 2;}return corner;}// The number of each side of each layer of the matrix is generated // s represents 4 directions, taking values 1, 2, 3, 4 respectively, representing 4 different directions. // o indicates the starting value of this edge. // x represents the number of numbers on each edge of the m-th layer private void side(int s, int o, int x, int m) {int i = 0;int j = 0;switch (s) {case 1: i = m - 1;j = m - 1;for (int k = 0; k < x; ++k) {a[i][j + k] = value;++value;}break;case 2: i = m - 1;j = m - 1 + x;for (int k = 0; k < x; ++k) {a[i + k][j] = value;++value;}break;case 3: i = m - 1 + x;j = m - 1 + x;for (int k = 0; k < x; ++k) {a[i + k][j] = value;++value;}break;case 3: i = m - 1 + x;j = m - 1 + x;for (int k = 0; k < x; ++k) {a[i + k][j] = value;++value;}break;case 3: i = m - 1 + x;j = m - 1 + x;for (int k = 0; k < x; ++k) {a[i][j - k] = value;++value;}break;case 4: i = m - 1 + x;j = m - 1;for (int k = 0; k < x; ++k) {a[i - k][j] = value;++value;}break;}}// Generate the m-th layer private void shell(int m)// m represents the mth layer {int x = n - 1 - (m - 1) * 2;// x represents the number of numbers in each edge of the mth layer int o = getcorner(m);int o1 = o;int o2 = o1 + x;int o3 = o2 + x;int o4 = o3 + x;// System.out.println(o4);side(1, o, x, m);side(2, o, x, m);side(3, o, x, m);side(4, o, x, m);}// Generate snake matrix public void snakeMatrix() {int m = (n + 1) / 2;// Calculate how many layers there are for (int i = 1; i <= m; ++i) {shell(i);}if (n % 2 == 1) {a[n / 2][n / 2] = n * n;}}// Print matrix public void print() {for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {if (a[i][j] < 10) {System.out.print(a[i][j] + " ");} else {System.out.print(a[i][j] + ");}}System.out.println();}}public static void main(String args[]) {mysnakematrix my = new mysnakematrix(new Scanner(System.in).nextint());//Use Scanner to get console input my.snakeMatrix();my.print();}}Summarize
The above is all the content of this article about Java programming to implement printing spiral matrix instance code, I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Java language description storage structure and adjacency matrix code example
Java programming implementation of adjacency matrix representation dense graph code example
Java programming to implement the complete code of A* algorithm
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!