将矩阵逆时针旋转45°后输出

【问题描述】
将矩阵逆时针旋转45°后输出。

【算法代码】

#include <iostream>
using namespace std;

const int maxn=110;
int matrix[maxn][maxn];

int main() {
    int n;
    while(cin>>n) {
        for(int i=0; i<=n-1; i++)
            for(int j=0; j<=n-1; j++)
                cin>>matrix[i][j];

        cout<<"The original matrix is as follows:"<<endl;
        for(int i=0; i<=n-1; i++) {
            for(int j=0; j<=n-1; j++) {
                cout<<matrix[i][j]<<" ";
            }
            cout<<endl;
        }

        cout<<"The matrix after 45° CCW rotation is:"<<endl;
        for(int i=n-1; i>=0; i--) {
            for(int j=0; j<=n-1; j++) {
                if(i+j<=n-1) {
                    cout<<matrix[j][i+j]<<" ";
                }
            }
            cout<<endl;
        }

        for(int i=1; i<=n-1; i++) {
            for(int j=0; j<=n-1; j++) {
                if(i+j<=n-1) {
                    cout<<matrix[i+j][j]<< " ";
                }
            }
            cout<<endl;
        }
    }

    return 0;
}


/*
in:
3
1 2 3
4 5 6
7 8 9

out:
3
2 6
1 5 9
4 8
7
*/



【参考文献】
https://www.cnblogs.com/radical/p/3985167.html
 

Guess you like

Origin blog.csdn.net/hnjzsyjyj/article/details/120662164