Rotating matrix code (c)
Address: http://blog.csdn.net/caroline_wendy
OutputRotation matrixTo make the MatrixDiagonal spiral riseAfter the output rule is determined, you can determine that the rising rule is,Add rows and columns as values.
Therefore, two loops are used, and the upper and lower matrices are used to separate the output.
For example:
1 2 6 7 15 3 5 8 14 16 4 9 13 17 22 10 12 18 21 23 11 19 20 24 25
Code:
/** Main. CPP ** created on: 2014.6.12 * Author: Spike * // * eclipse CDT, GCC 4.8.1 */# include <stdio. h> void output (int n) {If (n <0) return; const int max = 100; int A [Max] [Max]; int min = 1; int max = N * n; // upper matrix for (INT I = 0; I <n; I ++) {for (Int J = 0; j <I + 1; j ++) if (I % 2 = 0) {A [I-j] [J] = min ++; A [n-1-i + J] [n-1-j] = max --;} else {A [J] [I-j] = min ++; A [n-1-j] [n-1-i + J] = max -- ;}for (INT I = 0; I <n; I ++) { (Int J = 0; j <n; j ++) printf ("% 3d % C", a [I] [J], j = n-1? '\ N': '') ;}} int main () {output (5); Return 0 ;}
Output:
1 2 6 7 15 3 5 8 14 16 4 9 13 17 22 10 12 18 21 23 11 19 20 24 25
Programming Algorithm-rotating matrix code (c)