After the numbers from 1 to 9 are filled, the numbers in each column in each row are added to the 15-square-lattice algorithm.
1 /// <summary> 2 /// nine cells algorithm 3 /// </summary> 4 public void NineTable () 5 {6 // create a Third-Order Matrix 7 int [,] arr = new int [3, 3]; 8 // row subscript 9 int a = 2; 10 // row subscript of 2nd rows 11 int B = 3/2; 12 // assign 13 for the Array (int I = 1; I <= 9; I ++) 14 {15 arr [a ++, B ++] = I; 16 17 // if I is a multiple of 3 18 if (I % 3 = 0) 19 {20 a = a-2; 21 B = B-1; 22} 23 else // If I is not a multiple of 3 24 {25 a = a % 3; 26 B = B % 3; 27} 28} 29 30 StringBuilder sb = new StringBuilder (); 31 // traverse the output of the 9-Cell 32 for (int I = 0; I <3; I ++) 33 {34 for (int j = 0; j <3; j ++) 35 {36 sb. append (arr [I, j]. toString () + ""); 37} 38 sb. append ("<br/>"); 39} 40 lbNineTable. text = "output nine cells: <br/>" + sb. toString (). trim (); 41} View Code
The running result is as follows:
Output:
4 9 2
3 5 7
8 1 6