做题思路:
程序清单:
#include <iostream>
#include <iomanip>
using namespace std;
#define N 4
void show(int a[][N])
{
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
cout << setw(4) << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void rotate(int a[][N])
{
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < i; ++j)
{
int temp = 0;
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
cout << "转置后结果如下:" << endl;
show(a);
for (int i = 0; i < N; ++i)
{
for (int j = N - 1; j >= N / 2; --j)
{
int temp = 0;
temp = a[i][j];
a[i][j] = a[i][N - j - 1];
a[i][N - j - 1] = temp;
}
}
cout << "逆序后得到旋转后的最终结果如下:" << endl;
show(a);
}
int main(void)
{
int a[N][N] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16} };
cout << "原矩阵如下所示:" << endl;
show(a);
rotate(a);
return 0;
}
运行结果: