/*
请用最少的额外空间将一个M*N的矩阵旋转90度.
*/
#include <iostream>
#include <stdio.h>
using namespace std;
const int N = 100;
void rotate_left(int a[N][N], int m, int n)
{
int c_i = 0, c_j = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j ++)
{
c_i = j;
c_j = n - i - 1;
cout << a[c_i][c_j] << " ";
}
cout << endl;
}
cout << endl;
}
void rotate_right(int a[N][N], int m, int n)
{
int c_i = 0, c_j = 0;
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
c_j = i;
c_i = m - j - 1;
cout << a[c_i][c_j] << " ";
}
cout << endl;
}
cout << endl;
}
int main()
{
freopen("data.txt", "r", stdin);
int n, m;
int a[N][N];
while(cin >> m >> n)
{
for(int i = 0; i < m; i ++)
for(int j = 0; j < n; j ++)
cin >> a[i][j];
cout << "Rotated 90 degrees to the left:\n";
rotate_left(a, m, n);
cout << "Rotated 90 degrees to the right:\n";
rotate_right(a, m, n);
}
fclose(stdin);
return 0;
}
旋转矩阵
最新推荐文章于 2023-06-29 22:45:36 发布