做题思路:
![这里写图片描述](https://img-blog.csdn.net/20170914171603792?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV0dV9DUkFZT04=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
程序清单:
#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;
}
运行结果:
![这里写图片描述](https://img-blog.csdn.net/20170818210717863?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV0dV9DUkFZT04=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)