转置矩阵的输出
程序员文章站
2024-03-17 17:23:40
...
转置矩阵的输出
可以任意指定行和列,并逐行输入原矩阵的元素。最后输出原矩阵和转置矩阵。
思路是用int**和int*
#include<iostream>
using namespace std;
int main() {
int h = 0, l = 0;
cout << "输入行数和列数" << endl;
cin >> h >> l;
int** x = new int*[h];
for (int i = 0; i < h ; i++)
{
x[i] = new int[l];
}
cout << "开始输入矩阵元素" << endl;
for (int i = 0; i < h;i++) {
for (int j = 0; j < l; j++)
{
cin >> x[i][j];
}
}
int count = 0;
cout << "原矩阵为" << endl;
for (int i = 0; i < h; i++) {
for (int j = 0; j < l-1; j++)
{
cout << x[i][j] << " ";
}
cout << x[i][l - 1] << endl;
}
int** y = new int* [l];
for (int i = 0; i < l; i++)
{
y[i] = new int[h];
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < l; j++)
{
y[j][i] = x[i][j];
}
}
cout << "转置矩阵为" << endl;
for (int i = 0; i < l; i++) {
for (int j = 0; j < h - 1; j++)
{
cout << y[i][j] << " ";
}
cout << y[i][h - 1] << endl;
}
delete[]x;
delete[]y;
return 0;
}
上一篇: 12:最长平台
下一篇: C#将参数传入python时遇到的问题