详细思路看注释

#include <iostream>
#include <vector>

using namespace std;

void circle(vector<vector<int>> &arr, int instR, int instC, int inendR, int inendC)
{
    //因为是旋转正方形矩阵,因此只需要起始位置在终止位置左上角即可
    //特殊情况
    if (instR > inendR || instC > inendC)
        return;
    //正常情况
    else
    {
        int stR = instR;
        int stC = instC;
        int endR = inendR;
        int endC = inendC;
        //按位置,一个一个进行替换
        /*
            1**2
            ****
            ****
            4**3

            4->1
            1->2
            2->3
            3->4

            然后依次往后顺延
            *1**
            ***2
            4***
            **3*

            4->1
            1->2
            2->3
            3->4

            ...
        */
        for (int i = 0; i < endR - stR; i++)
        {
            int tem = arr[stR][stC+i];
            arr[stR][stC+i] = arr[endR-i][stC];
            arr[endR-i][stC] = arr[endR][endC-i];
            arr[endR][endC-i] = arr[stR+i][endC];
            arr[stR+i][endC] = tem;
        }
        circle(arr, instR + 1, instC + 1, inendR - 1, inendC - 1);
    }
}

int main()
{
    int n;//首先接收当前的输入数组
    while (cin >> n)
    {
        vector<vector<int>>arr(n, vector<int>(n,0));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cin >> arr[i][j];
            }
        }
        //进行逆时针旋转
        circle(arr,0,0,n-1,n-1);
        //按顺序输出到屏幕上
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cout << arr[i][j] << " ";
            }
            cout << endl;
        }
    }
    return 0;
}