旋转矩阵算法
2010-04-08 05:14 Logic0 阅读(2795) 评论(0) 编辑 收藏 举报旋转矩阵算法
问题:
打印形如
1 8 7
2 9 6
3 4 5
的N阶矩阵
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m = 0;
int now_n = 1;
int left,right;
int up,down;
int i = 1,j = 1;
int a[10][10];
printf("input your m:");
scanf("%d",&m);
printf("m is %d\n",m);
left = 1;
right = m;
up = 1;
down = m;
while(left <=right && up <= down)
{
for(i = left ; i <= right ; i++)
a[i][j] = now_n++;
up += 1;
i -= 1;
printf("now_n is %d\n",now_n);
for(j = up ; j <= down ; j++)
a[i][j] = now_n++;
right -= 1;
j -= 1;
for(i = right ; i >= left ; i--)
a[i][j] = now_n++;
down -= 1;
i += 1;
for(j = down ; j >= up ; j--)
a[i][j] = now_n++;
left += 1;
j += 1;
}
for(i = 1 ; i <= m ; i++)
{
for(j = 1 ; j <= m ; j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
system("pause");
return 0;
}