首页 > 代码库 > 逆转矩阵

逆转矩阵

1 2 3
4 5 6
7 8 9
转成
3 6 9
2 5 8 
1 4 7
 
#include "stdafx.h"#include <iostream>#include <vector>using namespace std;int _tmain(int argc, _TCHAR* argv[]){    vector<vector<int>>a(4, vector < int>(3));    int v = 1;    //初始化一个矩阵。    for (int i = 0; i < a.size(); i++){        for (int j = 0; j < a[0].size(); j++){            a[i][j] = v++;        }    }    vector<vector<int>>b =a;    //使用b保存矩阵a    for (int i = 0; i < b.size(); i++){        for (int j = 0; j < b[0].size(); j++){            cout << b[i][j] << "  ";         }        cout << endl;    }        //初始化矩阵的空间    a.clear();    a.resize(b[0].size());    for (int i = 0; i < a.size(); i++){        a[i].resize(b.size());    }    //制作逆时针转向的新矩阵    for (int i = 0; i < b[0].size(); i++){        for (int j = 0; j < b.size(); j++){            //a[i][j]=b[j][i];            a[b[0].size() - i - 1][j] = b[j][i];        }    }    //打印逆转矩阵的值    for (int i = 0; i < a.size(); i++){        for (int j = 0; j < a[0].size(); j++){            cout << a[i][j] << "  ";        }        cout << endl;    }    return 0;}

技术分享

 

逆转矩阵