(算法)求矩阵转置

题目:

编写一个函数,输入为一个矩阵,打印这个矩阵转置后的结果。
例:
输入矩阵是
1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16
 
打印结果应该是
13,9,5,1
14,10,6,2
15,11,7,3
16,12,8,4

思路:

1、从外到内,一圈一圈地循环替换;

2、对矩阵进行从[i,j]到[j,i]的交换,然后在进行列的首尾交换;

代码:

#include<iostream>
#include<vector>

using namespace std;

void RotateMatrix_1(vector<vector<int> > &matrix,int n){
    for(int layer=0;layer<n/2;++layer){
        int first=layer;
        int last=n-1-layer;
        for(int i=first;i<last;++i){
            int offset=i-first;
            // store top
            int top=matrix[first][i];

            // left to top
            matrix[first][i]=matrix[last-offset][first];

            // bottom to left
            matrix[last-offset][first]=matrix[last][last-offset];

            // right to bottom
            matrix[last][last-offset]=matrix[i][last];

            // top to right
            matrix[i][last]=top;
        }
    }

}

void swap(int &a,int &b){
    int tmp=a;
    a=b;
    b=tmp;
}

void RotateMatrix_2(vector<vector<int> > &matrix,int n){
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++)
            swap(matrix[i][j],matrix[j][i]);
    }    
    /*
    // Reverse clock-wise   
    for(int i=0;i<n/2;i++){
        for(int j=0;j<n;j++)
            swap(matrix[i][j],matrix[n-1-i][j]);
    }
    */

    // clock-wise
    for(int i=0;i<n/2;i++)
        for(int j=0;j<n;j++)
            swap(matrix[j][i],matrix[j][n-1-i]);
}

int main(){
    vector<vector<int> > matrix={
        {1,2,3,4},
        {5,6,7,8},
        {9,10,11,12},
        {13,14,15,16}
    };

    int n=matrix.size();

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

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
            cout<<matrix[i][j]<<" ";
        cout<<endl;
    }
    
    return 0;
}
原文地址:https://www.cnblogs.com/AndyJee/p/4865176.html