Rotation Matrix Code (C)
This article address: Http://blog.csdn.net/caroline_wendy
Output rotation matrix , so that the matrix is a diagonal spiral up , after the output rule is determined, it can be judged, the rising rule is that the row and column added to the fixed value .
So we use the method of two loops and the upper and lower matrices to separate output.
Such as:
1 2 6 7
3 5
8 4 9 25 (in)
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 = m;
int A[max][max];
int min = 1;
int max = n*n;
The last half 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++) {
for (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
3 5 8 4 9 22 25 (in)