Java and JavaScript to implement the extended nine-cell Algorithm

Source: Internet
Author: User

Requirement: According to the input number N, such as: 3, 5, 7... to display n rows of n columns in a matrix, these numbers are from 1 ~ N * n: The sum of N numbers on each column of the Matrix and the diagonal line must be equal.
Prerequisites:
In the matrix, 1 is in the middle of the first line, and the subsequent numbers should be placed in the upper-right square of the previous number. If not, they will be placed in the bottom pane of the column; if not, place it to the far left of the row. If not, place it to the front and bottom of the previous number. If there is already a number in the target grid, put it to the front and bottom of the previous number.

Ideas:
1) Use a 2-dimensional array to prepare for storage ~ N * n
2) 1 is placed in the center of the first row, so the index number is: [0] [(n-1)/2]
3) for subsequent numbers, the index number principle is as follows:
1> the row index of num is num-1 (row index-1), and the column index of num is num-1 (column index + 1)
2> If the row and column indexes of num are found to be out of BITs (-1 or N ),
The row index of num is num-1 (row index + 1), and the column index of num is num-1 (column index)
3> If the row of num is found and the column index points to a number
The row index of num is num-1 (row index + 1), and the column index of num is num-1 (column index)
4> If the row bitwise of num is found (-1 ),
Row indexes of num are n-1.
5> If the column of num is found to be n
The column index of num is 0.

The following is the implementation of Java and JavaScript.

**************************************** **************************************** ***********

Grid9.java
---------------------------------------------------

Import java. util. collections;

Class grid9 {
Public static void main (string [] ARGs ){
Pipeline SC = new pipeline (system. In );
System. Out. println ("Enter the number of rows :");
Int n = SC. nextint ();
System. Out. println ();
// N must be an odd number
If (N % 2 = 0)
N --;
// N must be greater than or equal to 3
If (n <3)
N = 3;
// Create an array --> similar to a double-layer set, which can be similar to a multi-dimensional array
// All members are initialized to 0
Int [] [] ary = new int [N] [N];
// The center of a row
Int mid = (n-1)/2;
// Current number
Int num = 1;
// Current row and column
Int currrow = 0;
Int currcol = mid;
// The next row and column
Int nextrow, nextcol;
While (Num <= N * n ){
Ary [currrow] [currcol] = num;
Num ++; // The next number.
Nextrow = currRow-1; // The row index of the next number
Nextcol = currcol + 1; // column index of the next number
// If the row and column indexes of the next number are invalid, the row index is changed to (the row index + 1 of the current number), and the column index is changed to (the column index of the current number)
If (nextrow <0 & nextcol> = N ){
Nextrow = currrow + 1;
Nextcol = currcol;
}
// If the row index of the next number is smaller than 0, change the row index to (5-1)
Else if (nextrow <0 ){
Nextrow = n-1;
}
// If the column index of the next number is equal to 5, the column index is changed to (0)
Else if (nextcol> = N ){
Nextcol = 0;
}
// If a value already exists at the place where the next number should be placed, the row index is changed to (the row index + 1 of the current number) and the column index is changed to (the column index of the current number)
Else if (ary [nextrow] [nextcol]> 0 ){
Nextrow = currrow + 1;
Nextcol = currcol;
}
// Change the current row to the next row to store the next number.
Currrow = nextrow;
Currcol = nextcol;
}
// Print the 9th percentile data
For (INT I = 0; I <n; I ++ ){
For (Int J = 0; j <n; j ++ ){
System. Out. Print (ary [I] [J] + "/t ");
}
System. Out. println ();
}
}
}

**************************************** **************************************** ***********

Grid9.html
---------------------------------------------------

<SCRIPT>
VaR n = prompt ("Enter the number of rows:", "3 ");
N = parseint (N );

// N must be an odd number
If (N % 2 = 0)
N --;

// N must be greater than or equal to 3
If (n <3)
N = 3;
// Create an array --> similar to a double-layer set, which can be similar to a multi-dimensional array
// All members are initialized to 0
VaR ary = new array ();
VaR aryline;
For (VAR I = 0; I <n; I ++ ){
Aryline = new array (N );
Ary. Push (aryline );
}
// The center of a row
VaR mid = (n-1)/2;

// Current number
VaR num = 1;

// Current row and column
VaR currrow = 0;
VaR currcol = mid;

// The next row and column
VaR nextrow, nextcol;

While (Num <= N * n ){
Ary [currrow] [currcol] = num;
Num ++; // The next number.
Nextrow = currRow-1; // The row index of the next number
Nextcol = currcol + 1; // column index of the next number
// If the row and column indexes are invalid, the row index is changed to (the row index of the current number + 1), and the column index is changed to (the column index of the current number)
If (nextrow <0 & nextcol> = N ){
Nextrow = currrow + 1;
Nextcol = currcol;
}
// If the row index of the next number is smaller than 0, change the row index to (5-1)
Else if (nextrow <0 ){
Nextrow = n-1;
}
// If the column index of the next number is equal to 5, the column index is changed to (0)
Else if (nextcol> = N ){
Nextcol = 0;
}
// If a value already exists at the place where the next number should be placed, the row index is changed to (the row index + 1 of the current number) and the column index is changed to (the column index of the current number)
Else if (ary [nextrow] [nextcol]! = NULL ){
Nextrow = currrow + 1;
Nextcol = currcol;
}
// Change the current row to the next row to store the next number.
Currrow = nextrow;
Currcol = nextcol;
}

Document. Write ("<Table border = '1'> ");
For (VAR I = 0; I <n; I ++ ){
Document. Write ("<tr> ");
For (var j = 0; j <n; j ++ ){
Document. Write ("<TD>" + ary [I] [J] + "</TD> ");
}
Document. Write ("</tr> ");
}
Document. Write ("</table> ");
</SCRIPT>

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.