注意!逆时针旋转
#include <iostream> using namespace std; #define N 9 //判断两个矩阵是否相同 int same(int a[][N],int b[][N],int na,int nb){ int i,j,flag = 1;//flag=1矩阵相同 for (i = 0;i < na;i++){ for (j = 0; j < na; j++){ if (a[i][j] != b[i][j]){ flag = 0; return flag; } } } return flag; } //输入 void input(int a[][N],int na){ int i,j; for (i = 0;i < na;i++){ for (j = 0; j < na; j++){ cin >>a[i][j]; } } } //旋转矩阵 void turn(int a[][N],int na){ int i,j,b[N][N]; for (i = 0;i < na;i++){ for (j = 0; j < na; j++){ b[i][j] = a[j][na-i-1]; } } for (i = 0;i < na;i++){ for (j = 0; j < na; j++){ a[i][j] = b[i][j]; } } } int main(){ int n; while (cin >>n && n != 0){ int a[N][N],b[N][N]; int i,j,count = 0;//count是旋转次数 //输入 input(a,n); input(b,n); //循环条件:矩阵不等 且 旋转次数小于4 while(!same(a,b,n,n) && count < 4){ turn(b,n); count++; } if(same(a,b,n,n)) cout <<count*90<<endl; else cout <<"-1"<<endl; } }