#include<iostream>
#include<stdio.h>
using namespace std;
#define N 2
/*int *rotate(int mat[][N])//顺时针旋转90度
{
for(int layer=0;layer<N/2;layer++)//层次
{
int last = N-1-layer;
for(int first=layer;first<last;first++)
{
int top = mat[layer][first];//左上顶点
mat[layer][first]=mat[N-1-first][layer];//左上顶点赋值
mat[N-1-first][layer]=mat[last][N-1-first];//左下顶点赋值
mat[last][N-1-first]=mat[first][last];//右下顶点赋值
mat[first][last]=top;//右上顶点赋值
}
}
return mat[0];
}
*/
void main()
{
int mat[][N] ={1,2,3,4};
for(int layer=0;layer<N/2;layer++)//层次
{
int last = N-1-layer;
for(int first=layer;first<last;first++)
{
int top = mat[layer][first];//左上顶点
mat[layer][first]=mat[N-1-first][layer];//左上顶点赋值
mat[N-1-first][layer]=mat[last][N-1-first];//左下顶点赋值
mat[last][N-1-first]=mat[first][last];//右下顶点赋值
mat[first][last]=top;//右上顶点赋值
}
}
//int *p = rotate(matrix);
int *p = mat[0];
for(int i = 0;i<(N^2);++i)
{
cout<<*(p+i)<<' '<<endl;
}
}
矩阵顺时针旋转90度
最新推荐文章于 2023-03-11 09:43:02 发布