矩阵旋转
描述
给你各一个n*n的矩阵,你的任务是将它顺时针旋转角度d。
输入
输入的第一个数为T,表示接下来有T组数据。
每组数据的格式如下:
第一行为两个整数n,d。1<=n<=100,d是90的倍数。
接下来有n行。每行n个整数,每个数之间由一个空格隔开,表示这个矩阵的元素。
输出
输出旋转后的矩阵,每个矩阵后面空一行,注意行末不要输出多余的空格。
输入样例 1
2
3 90
1 2 3
4 5 6
7 8 9
4 270
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
输出样例 1
7 4 1
8 5 2
9 6 3
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
Solution
模拟
Code
int T,n,d;
const int N = 105;
int a[N][N],b[N][N];
int main(){
cin >> T;
while(T --){
cin >> n >> d;
memset(b,0,sizeof b);
memset(a,0,sizeof a);
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
cin >> a[i][j];
d %= 360; //可能大于360
if(d == 0){
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ){
cout << a[i][j];
if(j != n)cout << " ";
}
cout << endl;
}
cout << endl;
}
else if(d == 90){
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ){
b[j][n - i + 1] = a[i][j];
}
}
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ){
cout << b[i][j];
if(j != n)cout << " ";
}
cout << endl;
}
cout << endl;
}
else if(d == 180){
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ){
b[n - i + 1][n - j + 1] = a[i][j];
}
}
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ){
cout << b[i][j];
if(j != n)cout << " ";
}
cout << endl;
}
cout << endl;
}
else{
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ){
b[n - j + 1][i] = a[i][j];
}
}
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ){
cout << b[i][j];
if(j != n)cout << " ";
}
cout << endl;
}
cout << endl;
}
}
return 0;
}