# Print digital ring pattern

Source: Internet
Author: User

Print case
16 15 14 13
5 4 3 12
6 1 2 11
7 8 9 10

You must dynamically change the number of rows and columns.

The following two methods are provided. I personally think the idea of method 2 is clearer, because we use recursion to fill in a circle of data every time until all circles of data are filled. in addition, when you fill in a circle of data, there is no need to fill in a number in a loop. In fact, as long as you cycle the data in the first row, you can calculate the data in the last row. As long as you cycle the data in the first column, the data of the last column can be calculated, which saves half of the cycle operation.

Method 1:

/**
*

This program can change the number of rows and columns to print
Principle: assign values one by one from the largest to the smallest by locating the row number and column number.

@ Author zhangle
*/
Public static void main (string [] ARGs ){

Int ROW = 4; // several rows
Int Col = 4; // Several columns
Int [] [] M = new int [row] [col]; // a two-dimensional array for storing results
Int n = Col * row; // maximum value in the Matrix
Int I = 0; // The row number.
Int J = 1; // column number
Int t_col = Col; // column Loop Variable
Int t_row = row; // The row loop variable.

While (true ){

// Assign a value to a row
For (int K = 0; k <t_col; k ++ ){
J + =-1;
M [math. Abs (I)] [math. Abs (j)] = n --;
}
If (n = 0) break; // exit if the value has been assigned
T_col --; // column loop --
T_row --; // row loop --

// Assign a value to a column
For (int K = 0; k <t_row; k ++ ){
I + =-1;
M [math. Abs (I)] [math. Abs (j)] = n --;
}

If (n = 0) break; // exit if the value has been assigned
J =-J;
I =-I;
}
Print (m );
}
/**
* Print a two-dimensional array
* @ Param
*/
Static void print (INT [] [] ){
For (INT I = 0; I <A. length; I ++ ){
For (Int J = 0; j <A [I]. length; j ++ ){
System. Out. Print ("" + A [I] [J]);
}
System. Out. println ();
}
}

Method 2:

Principle: first fill in the data in the outermost ring. If there is still inner ring data, the recursive call itself continues to fill in inner ring data until it is filled out.

Public class round {

Public static void main (string [] ARGs ){
Int ROW = 10; // several rows
Int Col = 10; // Several columns
Int [] [] A = new int [row] [col];

Fill (A, row, Col, 0, 0 );
Print ();
}

/**
* Enter the data in the outermost ring. If there is any inner ring, the recursive call itself continues to enter inner ring data until it is filled out.
* @ Param
* @ Param row
* @ Param col
* @ Param I
* @ Param J
*/
Private Static void fill (INT [] [] A, int row, int Col, int I, Int J ){

Int n = row * Col;
For (int c = 0; C <= col-1; C ++) {// horizontal fill Value
A [I] [J + C] = n-C; // enter the value from left to right in the first line.
If (row! = 1)
A [I + row-1] [J + C] = N-(COL * 2 + row-3) + C; // if there are more than one row, enter a value from left to right in the last line.
}

For (INT r = 0; r <row-2; r ++) {// vertical fill Value
A [I + row-2-r] [J] = A [I + row-1] [J]-R-1; // enter a value from bottom up In the first column
If (Col! = 1) // if there are more than one column, fill in the value of the big last column from bottom to top
A [I + row-2-r] [J + col-1] = A [I + row-1] [J + col-1] + R + 1;
Else // otherwise overwrite the first column of data
A [I + row-2-r] [J] = A [I + row-1] [J] + R + 1;
}

If (Row-2> 0 & col-2> 0) Fill (A, row-2, col-2, I + 1, J + 1 ); // if the data has not been filled in, enter it recursively
}

/**
* Print a two-dimensional array
* @ Param
*/
Private Static void print (INT [] [] ){

For (INT I = 0; I <A. length; I ++ ){
For (Int J = 0; j <A [0]. length; j ++ ){
System. Out. printf ("% 4D", a [I] [J]);
}
System. Out. println ();
}
}
}

Related Keywords:

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.