输入有多组数据。 每组数据第一行输入n(1<=n<=9),从第二行开始输入两个n阶矩阵。
判断第二个是否是第一个的旋转矩阵,如果是,输出旋转角度(0、90、180、270),如果不是,输出-1。 如果旋转角度的结果有多个,则输出最小的那个。
3 1 2 3 4 5 6 7 8 9 7 4 1 8 5 2 9 6 3
90
#include <cstdio> int a[9][9], b[9][9]; int main () { int n; scanf("%d", &n); for (int i=0; i<n; ++i) for (int j=0; j<n; ++j) scanf("%d", &a[i][j]); for (int i=0; i<n; ++i) for (int j=0; j<n; ++j) scanf("%d", &b[i][j]); if (**a == **b) printf("0"); else if (a[1][2] == b[2][n-2]) printf("90"); else if (a[1][2] == b[n-2][n-3]) printf("180"); else if (a[1][2] == b[n-3][1]) printf("270"); else printf("-1"); }(钻空子的代码)
//一楼的答案真的很简洁了 #include<iostream> using namespace std; int main() { int n; while (cin >> n) { int **a = new int*[n], **b = new int*[n], **c = new int*[n],**d = new int*[n],**e = new int*[n]; for (int i = 0; i<n; i++) { a[i] = new int[n]; b[i] = new int[n]; c[i] = new int[n]; d[i] = new int[n]; e[i] = new int[n]; } for (int i = 0; i<n; i++) for (int j = 0; j<n; j++) cin >> a[i][j]; for (int i = 0; i<n; i++) for (int j = 0; j<n; j++) cin >> e[i][j]; for (int i = 0; i<n; i++) for (int j = n - 1; j >= 0; j--) b[i][n - 1 - j] = a[j][i]; for (int i = 0; i<n; i++) for (int j = n - 1; j >= 0; j--) c[i][n - 1 - j] = b[j][i]; for (int i = 0; i<n; i++) for (int j = n - 1; j >= 0; j--) d[i][n - 1 - j] = c[j][i]; bool judge = 1; for (int i = 0; i < n&&judge; i++) for (int j = 0; j < n; j++) if (a[i][j] != e[i][j]) { judge = 0; break; } if (judge) cout << "0" << endl; else { judge = 1; for (int i = 0; i < n&&judge; i++) for (int j = 0; j < n; j++) if (b[i][j] != e[i][j]) { judge = 0; break; } if (judge) cout << "90" << endl; else { judge = 1; for (int i = 0; i < n&&judge; i++) for (int j = 0; j < n; j++) if (c[i][j] != e[i][j]) { judge = 0; break; } if (judge) cout << "180" << endl; else { judge = 1; for (int i = 0; i < n&&judge; i++) for (int j = 0; j < n; j++) if (d[i][j] != e[i][j]) { judge = 0; break; } if (judge) cout << "270" << endl; else cout << "-1" << endl; } } } } }
通过这道题目,我学会了new二维数组以及-1要用双引号
。。。
好了我知道丢人了
#include<iostream>
using namespace std;
int n;
int **rot(int a[9][9], int angle){
int **b = new int*[9];
for(int i = 0; i < 9; i++){
b[i] = new int[9];
}
if(angle == 1){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
b[j][n - i - 1] = a[i][j];
}
}
}
else if(angle == 2){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
b[n - i - 1][n - j - 1] = a[i][j];
}
}
}
else if(angle == 3){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
b[n - j - 1][i] = a[i][j];
}
}
}
else{
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
b[i][j] = a[i][j];
}
}
}
return b;
}
bool mysame(int a[9][9], int **b){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(a[i][j] != b[i][j]){
return false;
}
}
}
return true;
}
int main(){
int a[9][9], b[9][9];
while(cin >> n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> a[i][j];
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> b[i][j];
}
}
bool flag = false, temp;
for(int angle = 0; angle < 4; angle++){
temp = mysame(b, rot(a, angle));
flag = flag || temp;
if(temp == true){
cout << 90 * angle << endl;
break;
}
}
if(flag == false){
cout << "-1" << endl;
}
}
return 0;
}
#include<iostream> using namespace std; int test1163() { int n; while(cin>>n) { bool v0=true,v90=true,v180=true,v270=true; int *m1=new int[n*n]; int *m2=new int[n*n]; int *m90=new int[n*n]; int *m180=new int[n*n]; int *m270=new int[n*n]; for(int i=0;i<n*n;i++) { cin>>m1[i]; } for(int j=0;j<n*n;j++) { cin>>m2[j]; } for(int a=0;a<n;a++) { for(int j=0;j<n;j++) { m90[n*a+j]=m1[n*(n-1-j)+a]; m180[n*a+j]=m1[n*(n-1-a)+(n-1-j)]; m270[n*a+j]=m1[n*j+(n-1-a)]; } } for(int b=0;b<n*n;b++) { if(m2[b]!=m1[b]) v0=false; if(m2[b]!=m90[b]) v90=false; if(m2[b]!=m180[b]) v180=false; if(m2[b]!=m270[b]) v270=false; } if(v0) cout<<"0"<<endl; else if(v90) cout<<"90"<<endl; else if(v180) cout<<"180"<<endl; else if(v270) cout<<"270"<<endl; else cout<<"-1"<<endl; delete []m1; delete []m2; delete []m90; delete []m180; delete []m270; } return 0; } int main() { test1163(); return 0; }
#include <iostream>
using namespace std;
int main() {
int N;
while (cin >> N) {
int A[N][N], B[N][N], flag[4] = { 0 };
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
cin >> A[i][j];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
cin >> B[i][j];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (A[i][j] != B[i][j])
flag[0] = 1;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (A[i][j] != B[j][N - i - 1])
flag[1] = 1;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (A[i][j] != B[N - i - 1][N - j - 1])
flag[2] = 1;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (A[i][j] != B[N - j - 1][i])
flag[3] = 1;
if (flag[0] == 0)
cout << "0" << endl;
else if (flag[1] == 0)
cout << "90" << endl;
else if (flag[2] == 0)
cout << "180" << endl;
else if (flag[3] == 0)
cout << "270" << endl;
else
cout << -1 << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int N;
while (cin >> N) {
int A[N][N], B[N][N], result = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
cin >> A[i][j];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
cin >> B[i][j];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
if (result == 0 && A[i][j] == B[i][j])
continue;
else {
result = 90;
if (result == 90 && (A[i][j] == B[j][N - i - 1]))
continue;
else {
result = 180;
if (result == 180 && (A[i][j] == B[N - i - 1][N - j - 1]))
continue;
else {
result = 270;
if (result == 270 && (A[i][j] == B[N - j - 1][i]))
continue;
else {
result = -1;
break;
}
}
}
}
}
cout << result << endl;
}
}
#include<stdio.h> void turn90(int A[10][10],int n){ //将A顺时针旋转90度后存储在A中 int B[10][10]; for(int i=0;i<n;i++){ //讲矩阵A顺时针旋转90度后暂存在B矩阵中 for(int j=0;j<n;j++){ B[i][j]=A[n-1-j][i]; } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ A[i][j]=B[i][j]; } } } int judge(int A[10][10],int B[10][10],int n){ //判断A,B两个矩阵是否相同 for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(A[i][j]!=B[i][j]) return 0; } } return 1; //如果相同则返回1; } int main(){ int n; while(scanf("%d",&n)!=EOF){ int buf_A[10][10]; int buf_B[10][10]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ scanf("%d",&buf_A[i][j]); } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ scanf("%d",&buf_B[i][j]); } } int tmp[10][10]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ tmp[i][j]=buf_A[i][j]; } } int cnt=0; for(int i=1;i<=4;i++){ int m=judge(buf_A,buf_B,n); if(m==1) { cnt=i; break; } turn90(buf_A,n); //将buf_A顺时针旋转90度放在tmp中 } if(cnt==0) printf("-1"); else if(cnt==1) printf("0"); else if(cnt==2) printf("90"); else if(cnt==3) printf("180"); else if(cnt==4) printf("270"); } return 0; }
#include<stdio.h> int main() { int n; int i,j; while(scanf("%d",&n)!=EOF) { int a[9][9],b[9][9]; for(i=0;i<n;i++) /**录入矩阵的值*/ for(j=0;j<n;j++) scanf("%d",&a[i][j]); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); int flag[4]={1,1,1,1}; /**标记0,90,180,270四个方向*/ for(i=0;i<n;i++) for(j=0;j<n;j++) { if(a[i][j]!=b[i][j]) flag[0]=0; if(a[n-1-j][i]!=b[i][j]) flag[1]=0; if(a[n-1-i][n-1-j]!=b[i][j]) flag[2]=0; if(a[j][n-1-i]!=b[i][j]) flag[3]=0; } i=0; while(flag[i]==0) i++; if(i==4) printf("-1\n"); else printf("%d\n",90*i); } return 0; }
//思路和之前一样,矩阵的旋转等同于先上下翻转,再沿着对角线对称。 #include <cstdio> (802)#include <iostream> #include <algorithm> using namespace std; // 顺时针旋转90度 void rotate(int (*a)[10], int len) { // 先上下翻转 int top = 0, down = len - 1; while (top < down) { for (int i = 0; i < len; ++i) { swap(a[top][i], a[down][i]); } top++, down--; } // 沿着对角线翻转 for (int i = 0; i < len; ++i) { for (int j = i+1; j < len; ++j) { swap(a[i][j], a[j][i]); } } } // 判断是否相同 bool isSame(int a1[10][10], int a2[10][10], int n) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if(a1[i][j] != a2[i][j]) return false; } } return true; } int arr1[10][10]; int arr2[10][10]; int main() { freopen("data.txt", "r", stdin); int n; scanf("%d", &n); int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { scanf("%d", &arr1[i][j]); } } for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { scanf("%d", &arr2[i][j]); } } int sec = 0; if(isSame(arr1, arr2, n)) { printf("%d", sec); return 0; } sec+=90; rotate(arr1, n); if(isSame(arr1, arr2, n)) { printf("%d", sec); return 0; } sec+=90; rotate(arr1, n); if(isSame(arr1, arr2, n)) { printf("%d", sec); return 0; } sec+=90; rotate(arr1, n); if(isSame(arr1, arr2, n)) { printf("%d", sec); return 0; } printf("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; bool is_90(int a[9][9],int b[9][9]) { for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(a[i][j]!=b[j][n-1-i]) { return false; } } return true; } bool is_180(int a[9][9],int b[9][9]) { for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(a[i][j]!=b[n-1-i][n-1-j]) { return false; } } return true; } bool is_equal(int a[9][9],int b[9][9]) { for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(a[i][j]!=b[i][j]) { return false; } } return true; } int main() { int n; while(cin>>n) { int a[9][9],b[9][9]; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a[i][j]; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>b[i][j]; if(is_equal(a,b)) { cout<<0<<endl; continue; } if(is_90(a,b)) { cout<<90<<endl; continue; } if(is_180(a,b)) { cout<<180<<endl; continue; } if(is_90(b,a))//a转270度得b,等价于b转90度得a { cout<<270<<endl; continue; } cout<<-1<<endl; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int n = scanner.nextInt(); int[][] arr = new int[n][n]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { arr[i][j] = scanner.nextInt(); } } //旋转90度 int[][] arr90 = new int[n][n]; for (int i = 0; i < arr90.length; i++) { for (int j = 0; j < arr90[i].length; j++) { arr90[i][j] = arr[n-j-1][i]; } } int[][] arr180 = new int[n][n]; for (int i = 0; i < arr180.length; i++) { for (int j = 0; j < arr180[i].length; j++) { arr180[i][j] = arr90[n-j-1][i]; } } int[][] arr270 = new int[n][n]; for (int i = 0; i < arr270.length; i++) { for (int j = 0; j < arr270[i].length; j++) { arr270[i][j] = arr180[n-j-1][i]; } } int[][] arrLast = new int[n][n]; for (int i = 0; i < arrLast.length; i++) { for (int j = 0; j < arrLast[i].length; j++) { arrLast[i][j] = scanner.nextInt(); } } if (isEqual(arrLast, arr)) { System.out.println(0); }else if (isEqual(arrLast, arr90)) { System.out.println(90); }else if (isEqual(arrLast, arr180)) { System.out.println(180); }else if (isEqual(arrLast, arr270)) { System.out.println(270); }else { System.out.println(-1); } } } public static boolean isEqual(int[][] arr,int[][] arr2) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { if (arr[i][j] != arr2[i][j]) { return false; } } } return true; } }
#include<stdio.h> int arr1[100][100]; int arr2[100][100]; bool IsSame(int N){ int flag = 1; for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ if(arr1[i][j]!=arr2[i][j]){ return false; } } } return true; } void Switch(int N){ int tmp[100][100]; for(int i=1;i<=N;i++){ for(int j=N;j>0;j--){ tmp[i][N-j+1] = arr1[j][i]; } } for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ arr1[i][j] = tmp[i][j]; } } } int main(){ int N; scanf("%d",&N); for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ scanf("%d",&arr1[i][j]); } } for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ scanf("%d",&arr2[i][j]); } } int count = 0; while(!IsSame(N)){ Switch(N); count+=90; if(count==360) break; } if(count<360) printf("%d\n",count); else printf("-1\n"); }
原地旋转
#include <iostream> #include <vector> #include <algorithm> using namespace std; void rotate(vector<vector<int>>& vv){ int n = vv.size(); int row = n / 2; int col = (n % 2 == 0) ? (n / 2) : (n / 2 + 1); for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j){ swap(vv[i][j], vv[j][n-i-1]); swap(vv[n-i-1][n-j-1], vv[n-j-1][i]); swap(vv[i][j], vv[n-i-1][n-j-1]); } } bool compare(const vector<vector<int>>& vv1, const vector<vector<int>>& vv2){ int n = vv1.size(); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (vv1[i][j] != vv2[i][j]) return false; return true; } int getRotateAngle(vector<vector<int>>& vv1, const vector<vector<int>>& vv2){ for (int i = 1; i <= 3; ++i){ rotate(vv1); if (compare(vv1, vv2)) return i * 90; } return -1; } int main() { int n; while (cin >> n){ vector<vector<int>> vv1(n, vector<int>(n)); vector<vector<int>> vv2(n, vector<int>(n)); for (auto &v : vv1) for (int &i : v) cin >> i; for (auto &v : vv2) for (int &i : v) cin >> i; if (compare(vv1, vv2)){ cout << 0 << endl; continue; } cout << getRotateAngle(vv1, vv2) << endl; } }
#include <stdio.h> #include <string.h> using namespace std; bool cmp(int *a, int *b, int len) { for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++) { // 利用数组的顺序存储从而访问数组元素 if (*(a + i * len + j) != *(b + i * len + j)) { return false; } } } return true; } int main() { int n; scanf("%d", &n); int mat_1[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &mat_1[i][j]); } } int mat_2[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &mat_2[i][j]); } } int degree = 0; bool flag = false; int temp[n][n]; // 依次旋转 0、90、180、270 度 for (int i = 0; i < 4; i++) { if (!cmp(&mat_1[0][0], &mat_2[0][0], n)) { // rotate for (int r = 0; r < n; r++) { for (int c = 0; c < n; c++) { temp[c][n - 1 - r] = mat_1[r][c]; } } // 将旋转后的矩阵 temp 赋值给 mat_1 memcpy(mat_1, temp, sizeof(temp)); // 累加旋转的度数 degree += 90; } else { flag = true; } } if (flag) printf("%d", degree); else printf("-1"); }
#include<iostream> #include<cstdio> using namespace std; const int N = 10; int matrix1[N][N], matrix2[N][N]; int Judge(int n){ bool flag = true; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(matrix1[i][j] != matrix2[i][j]){ flag = false; break; } } if(!flag) break; } if(flag) return 0; flag = true; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(matrix1[i][j] != matrix2[j][n - i - 1]){ flag = false; break; } } if(!flag) break; } if(flag) return 90; flag = true; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(matrix1[i][j] != matrix2[n - i - 1][n - j - 1]){ flag = false; break; } } if(!flag) break; } if(flag) return 180; flag = true; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(matrix1[i][j] != matrix2[n - j - 1][i]){ flag = false; break; } } if(!flag) break; } if(flag) return 270; return -1; } int main(){ int n; while(scanf("%d", &n) != EOF){ for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%d", &matrix1[i][j]); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%d", &matrix2[i][j]); printf("%d\n", Judge(n)); } return 0; }
#include <iostream> #include <vector> using namespace std; int n = 0; void inPut(vector<vector<int>>& Matrix) { int num = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { cin >> num; Matrix[i][j] = num; } } } void ROTATE(vector<vector<int>>& Matrix) { vector<vector<int>> ans(n, vector<int>(n)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { ans[j][n - 1 - i] = Matrix[i][j]; } } Matrix = ans; } bool isEqual(const vector<vector<int>>& A, const vector<vector<int>>& B) { int m = A.size(); if(m == 0 || m != B.size()) { return false; } int n = A[0].size(); if(n == 0 || n != B[0].size()) { return false; } for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(A[i][j] != B[i][j]) { return false; } } } return true; } int main() { //INIT cin >> n; vector<int> Angle{0,90,180,270,-1}; vector<vector<int>> MatrixA(n, vector<int>(n)); vector<vector<int>> MatrixB(n, vector<int>(n)); inPut(MatrixA); inPut(MatrixB); for(int i = 0; i <= 4; i++) { if(i == 4) { cout << Angle[i]; } if(isEqual(MatrixA, MatrixB)) { cout << Angle[i]; break; } ROTATE(MatrixA); } return 0; }
#include <cstdio> #include <algorithm> #include <iostream> using namespace std; int main(){ int n ; while(cin>>n){ int ma[n][n], mb[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++) cin>>ma[i][j]; } //对ma进行转置运算 for(int i = 0; i < n; i++){ for(int j = i+1; j < n; j++) swap(ma[i][j], ma[j][i]); } int flag90 = 0, flag180 = 0, flag270 = 0, flag0=0; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cin>>mb[i][j]; if(mb[i][j]==ma[j][i]) flag0++; //90度与转置后的矩阵左右对称 if(mb[i][j]==ma[i][n-j-1]) flag90++; if(mb[i][j]==ma[n-j-1][n-i-1]) flag180++; //270度与转置后的矩阵上下对称 if(mb[i][j]==ma[n-i-1][j]) flag270++; } } if(flag0==n*n||flag90==n*n||flag270==n*n||flag180==n*n){ int temp = flag0==n*n?0:(flag90==n*n?90:(flag180==n*n?180:270)); cout<<temp<<endl; } else cout<<-1<<endl; } }
#include <iostream> using namespace std; int arr1[10][10] = {0}; int arr2[10][10] = {0}; int temp[10][10] = {0}; void rev(int n){ int index = n - 1; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ temp[j][index] = arr1[i][j]; } index--; } for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ arr1[i][j] = temp[i][j]; } } } bool tojudge(int n){ for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(arr1[i][j] != arr2[i][j]) return false; } } return true; } int main(){ int n; while(cin >> n){ for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ cin >> arr1[i][j]; } } for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ cin >> arr2[i][j]; } } if(tojudge(n)){ cout << "0" << endl; continue; } bool f = false; for(int i = 1;i <= 3;i++){ rev(n); if(tojudge(n)){ f = true; cout << i * 90 << endl; break; } } if(f == false) cout << "-1" << endl; } }
#include<iostream> using namespace std; int main(){ int n; while(cin>>n){ int a[9][9],b[9][9]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>a[i][j]; } } bool o=true; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>b[i][j]; if(b[i][j]!=a[i][j]) o=false; } } //90 第0行等于最后一列,第1行等于倒数第二列,最后一行等于第一列 bool ninty=true; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(a[i][j]!=b[j][n-i-1]){ ninty=false; break; } } } //180 第0行等于最后一行倒着,第1行等于倒数第二行倒着,最后一行等于第一行倒着 bool oneeighto=true; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(a[i][j]!=b[n-i-1][n-j-1]){ oneeighto=false; break; } } } //180 第0行等于第0列倒着,第1行等于第1列倒着 bool twoseveno=true; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(a[i][j]!=b[n-j-1][i]){ twoseveno=false; break; } } } if(o==true){ cout<<0<<endl; }else if(ninty==true){ cout<<90<<endl; }else if(oneeighto==true){ cout<<180<<endl; }else if(twoseveno==true){ cout<<270<<endl; }else{ cout<<-1<<endl; } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); int[][] arr1 = new int[n][n]; int[][] arr2 = new int[n][n]; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ arr1[i][j] = sc.nextInt(); } } for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ arr2[i][j] = sc.nextInt(); } } whichAngle(arr1, arr2, n); } } static int[][] rotate90(int[][] arr, int n){ int[][] rArr = new int[n][n]; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ rArr[i][j] = arr[n-1-j][i]; } } return rArr; } static boolean isEqual(int[][] arr1, int [][] arr2, int n){ for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ if (arr1[i][j] != arr2[i][j]){ return false; } } } return true; } static void whichAngle(int[][] arr1, int[][] arr2, int n){ int[][] arr90 = rotate90(arr1, n); int[][] arr180 = rotate90(arr90, n); int[][] arr270 = rotate90(arr180, n); if (isEqual(arr1, arr2, n)){ System.out.println("0"); return; } else if (isEqual(arr90, arr2, n)){ System.out.println("90"); return; } else if (isEqual(arr180, arr2, n)){ System.out.println("180"); return; } else if (isEqual(arr270, arr2, n)){ System.out.println("270"); return; } else { System.out.println("-1"); return; } } }
#include<cstdio> #include<vector> using namespace std; int main(){ int n; while(scanf("%d",&n)!=EOF){ vector<vector<int>> v(n,vector<int>(n)),v1(n,vector<int>(n)),v2(n,vector<int>(n)); for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ scanf("%d",&v[i][j]); } } int flag=0; for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ scanf("%d",&v1[i][j]); if(v[i][j]!=v1[i][j]) { flag=1; } } } if(flag==0) { printf("0\n"); continue; } int k; for(k=0;k<3;++k){ flag=0; for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ if(v[j][i]!=v1[i][n-j-1]) { flag=1; } v2[i][n-j-1]=v[j][i]; } } if(flag==0){ printf("%d\n",(k+1)*90); break; } v=v2; } if(k==3) printf("-1\n"); } return 0; }