Array-13. Spiral phalanx, array-13 spiral phalanx
1/* 2 * Main. c 3 * E13-array-13. 4 * Created on: august 25, 2014*7 */8 9 # include <stdio. h> 10 11 int main (void) {12 13 int array [11] [11]; 14 int n; // N15 16 scanf ("% d ", & n); 17 18 int upBound = 0; // the upper boundary 19 int bottomBound = n-1; // The lower boundary 20 int leftBound = 0; // The left boundary 21 int rightBound = n-1; // The right boundary 22 int num = 1; // The Matrix value, starting from 1 to 23 int I; 24 25 while (upBound <= bottomBound & leftBound <= rightBound) {26 for (I = leftBound; I <= rightBound; I ++) {27 array [upBound] [I] = num; 28 num ++; 29} 30 if (upBound = bottomBound) 31 break; 32 33 upBound ++; // move a row up to 34 for (I = upBound; I <= bottomBound; I ++) {35 array [I] [rightBound] = num; 36 num ++; 37} 38 if (leftBound = rightBound) 39 break; 40 41 rightBound --; // shift the right border to a row 42 for (I = rightBound; I >= leftBound; I --) {43 array [bottomBound] [I] = num; 44 num ++; 45} 46 47 bottomBound --; // move the world one row down 48 for (I = bottomBound; I >= upBound; I --) {49 array [I] [leftBound] = num; 50 num ++; 51} 52 53 leftBound ++; // shifts the left border to a row 54 55} 56 // output matrix 57 int j; 58 for (I = 0; I <n; I ++) {59 for (j = 0; j <n; j ++) {60 printf ("% 3d", array [I] [j]); 61} 62 printf ("\ n "); 63} 64 65 return 0; 66}
***
Here, I declare that this question is the C language version I implemented based on "YangKang", but because I cannot find the URL, I will attach the "YangKang" code below. If you know the specific website, please let me know and I will add the source.
***
1/** 2 * Problem description: 3 * input an integer from the keyboard, and 4 * uses this number as the size of the matrix, putting 1, 2, 3 ,..., The numbers of n * n are entered in clockwise spiral form. 5 * For example: 6 * input number 2, program output: 7*1 2 8*4 3 9 * input number 3, program output: 10*1 2 311*8 9 412*7 6 513 * input number 4, then the program outputs: 14*1 2 3 415*12 13 14 516*11 16 15 617*10 9 8 718*19 * @ author YangKang20 * 21 */22 import java. util. california; 23 24 public class Main {25 public static void main (String [] args) {26 California keyin = new California (System. in); 27 28 System. out. println ("Enter the matrix size"); 29 int n = keyin. nextInt (); 30 if (N = 0) 31 System. exit (0); 32 int result [] [] = new int [n] [n]; // store the final spiral number square matrix 33/* 34 *. in the program, the data is first stored in the result from the row 0 in the previous step. When the right boundary N-1 is reached, the previous pointer moves down one row, and 35 * Other boundary pointers do not move. At this time, the row loop variable moves down one row at a time, and the column loop Pointer Points to the right boundary and continues to insert incremental numbers, 36 */37 int upBound = 0; // previous pointer 38 int downBound = n-1; // lower bound pointer 39 int leftBound = 0; // left boundary pointer 40 int rightBound = n-1; // right boundary pointer 41 int num = 1; // record number value, each insert one, auto increment 42 while (upBound <= downBound & leftBound <= rightBound) {43/* 44 * Insert numbers from left to right into the array position from small to large. J indicates the column loop variable 45 */46 for (int j = leftBound; j <= rightBound; j ++) {47 result [upBound] [j] = num; 48 num ++; // number record auto increment 49} 50 if (upBound = downBound) // avoid repeated insertion of line 51 break; 52/* 53 * Insert the top row in the circle and insert the right column into the array. The direction is from top to bottom. At this time, the previous pointer should move down one row. The other pointers will not move, and the row loop variable will move down layer by layer. 54 * I indicates the row loop variable 55 */56 upBound ++; // moves down a row 57 for (int I = upBound; I <= downBound; I ++) {58 result [I] [rightBound] = num; 59 num ++; 60} 61 if (leftBound = rightBound) // avoid repeated insertion of column 62 break; 63/* 64 * after the right boundary column is inserted, a number is inserted into the downward boundary line, and the direction is from right to left. At this time, the right boundary pointer moves one row to the left, and the other pointers do not move, and the column loop variables move one by one. 65 * j indicates the column loop variable 66 */67 rightBound --; // shift left for a row 68 for (int j = rightBound; j> = leftBound; j --) {69 result [downBound] [j] = num; 70 num ++; 71} 72/* 73 * after inserting the bottom boundary row in a circle, start to insert a column number into the left boundary, the direction is from bottom to top. 74 * at this time, the bottom boundary pointer moves up a row. Other pointers do not move, and the row loop variable moves up layer by layer. I Represents the row loop variable 75 */76 downBound --; // moves a row 77 for (int I = downBound; I> = upBound; I --) {78 result [I] [leftBound] = num; 79 num ++; 80} 81/* 82 * The Insert Process in this lap is complete, reproduce the continue and start the Insert Process in the next lap. At this time, the left boundary pointer needs to be adjusted again to move a row to the right. 83 */84 leftBound ++; // move the left border to the right of a row 85} 86 87/* 88 * output matrix spiral array 89 */90 for (int I = 0; I <n; I ++) {91 for (int j = 0; j <n; j ++) {92 System. out. print (result [I] [j] + "\ t"); 93} 94 System. out. println (); 95} 96} 97}
Question link:
Http://pat.zju.edu.cn/contests/basic-programming/%E6%95%B0%E7%BB%84-13
.
Javascript uses two arrays to print the spiral square matrix of the n-order rectangle, as shown in n = 5 9 8 7
First of all, it should be clear that there is no two-dimensional array concept in JavaScript, and only common Arrays can be used to simulate the effect of two-dimensional arrays.
Secondly, we can analyze two-dimensional arrays to obtain the law of the number of cycles, that is, after an even number/2 or an odd number is rounded to 2, 1 is added.
Again, we can see that the square matrix is gradually increased from the bottom right corner and counter-clockwise.
You can write the following functions:
Function printAll (number, start ){
// Number indicates the N-order matrix, and start indicates the Starting number. The default values are 5 and 1, respectively.
Number = number | 5;
Start = start | 1;
// C indicates the number of cycles.
Var c = number % 2 = 0? Number/2: Math. ceil (number/2 );
// Define a one-dimensional array for easy generation of two-dimensional arrays
Var arr = [];
// Generate a two-dimensional array and initialize it. The value is 0.
For (var I = 0; I <number; I ++ ){
Arr [I] = [];
For (var j = 0; j <number; j ++ ){
Arr [I]. push (0 );
}
}
// Function with one loop
Function circle (c ){
// Assign values from bottom to top
For (I = number-c-1; I> = c; I --){
Arr [I] [number-c-1] = start ++;
}
// Assign values from right to left
For (I = number-c-2; I> = c; I --){
Arr [c] [I] = start ++;
}
// Assign values from top to bottom
For (I = c + 1; I <number-c; I ++ ){
Arr [I] [c] = start ++;
}
// Assign values from left to right
For (I = c + 1; I <number-c-1; I ++ ){
Arr [number-c-1] [I] = start ++;
}
}
// Cyclically assign values to the array
For (j = 0; j <c; j ++ ){
Circle (j );
}
// View the generated array information on the console, which can be commented out.
For (I = 0; I <number; I ++ ){
Console. log (arr [I]);
}
}
I hope I can help you with the ideas and functions... the rest is full>
Spiral phalanx
Main ()
{
Int I, j, k, m = 1, n, a [100] [100];
J = 0; k = 0;
Scanf ("% d", & n );
While (k <= (n + 2)/2)
{
J = k;
For (I = k; I <= n-k-1; I ++)
{
A [I] [j] = m;
M ++ ;}
I = n-k-1;
For (j = k + 1; j <= n-k-1-1; j ++)
{
A [I] [j] = m; m ++;
}
J = n-k-1;
For (I = n-k-1; I> = k; I --)
{
A [I] [j] = m; m ++ ;}
I = k;
For (j = n-k-1-1; j> = k + 1; j --)
{A [I] [j] = m; m ++ ;}
K ++;
}
If (n % 2! = 0)
A [(n-1)/2] [(n-1)/2] = a [(n-1)/2] [(n-1)/2]-1;
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
Printf ("% 5d", a [I] [j]);
Printf ("\ n ");
}
}