# 把N*N矩阵转90度的算法与感想

xiaoxiao2021-02-28  12

下部分a[last][last-offect]

左部分： a[last-offect][first]

右部分:a[i][last]

#include <iostream>

#include <iomanip> using namespace std; #define N 2 void reverseRectangle(int a[N][N]) { int i,offect,layer,first,last,top; for(layer=0;layer< N/2;layer++) { first = layer; last = N-1 - layer; for(i = first;i<last;i++) { offect = i - first; top = a[first][i];  //save top a[first][i] = a[last-offect][first];           //left -> top a[last-offect][first] =  a[last][last - offect];                  // button -> left a[last][last - offect] = a[i][last] ; // right -> button a[i][last] = top;        // top -> right  } } cout<<"the reverseRectangle is complished"<<endl<<endl; } int main() { int a[N][N],i,j,(*p)[N]; p = a; //初始化a[][]并打印 for(i=0;i<N;i++) { for(j=0;j<N;j++) { *(*(p+i)+j) = i*N+j+1; cout<<setw(7)<<a[i][j]; } cout<<endl; } //矩阵逆转 ,q reverseRectangle(a); //打印逆转的矩形  for(i=0;i<N;i++) { for(j=0;j<N;j++) { cout<<setw(7)<<a[i][j]; } cout<<endl; } return 0;

}