question
![由于显示问题,输入示例的问号是空格](https://img-blog.csdnimg.cn/7a24ee694e67415b94c86814853665b3.png)
完整代码
#include <iostream>
#include <vector>
#define MAX_SIZE 400
struct Trida
{
int row, col;
int val;
};
struct SparseMatrix
{
Trida data[MAX_SIZE];
int m, n, len;
};
void buildSparseMatrix(SparseMatrix &Mat);
void transpose1(SparseMatrix Source, SparseMatrix &Dest);
void transpose2(SparseMatrix Source, SparseMatrix &Dest);
void showSparseMatrix(SparseMatrix Mat);
void SparsetoNormal(SparseMatrix Mat);
int main()
{
SparseMatrix M, T1, T2;
int m, n;
std::cin >> m >> n;
M.m = m, M.n = n;
buildSparseMatrix(M);
for (auto i = 0; i < M.len; ++i)
{
T1.data[i].row = 0;
T1.data[i].col = 0;
T1.data[i].val = 0;
T2.data[i].row = 0;
T2.data[i].col = 0;
T2.data[i].val = 0;
}
std::cout << "--------------------------------------" << std::endl;
SparsetoNormal(M);
std::cout << "--------------------------------------" << std::endl;
transpose1(M, T1);
SparsetoNormal(T1);
std::cout << "--------------------------------------" << std::endl;
transpose2(M, T2);
SparsetoNormal(T2);
std::cout << "T1--------------------------------------" << std::endl;
showSparseMatrix(T1);
std::cout << "T2--------------------------------------" << std::endl;
showSparseMatrix(T2);
system("pause");
return 0;
}
void buildSparseMatrix(SparseMatrix &Mat)
{
int row, col, val, cnt = 0;
while (true)
{
std::cin >> row >> col >> val;
if (row == 0 && col == 0 && val == 0)
break;
else
{
Mat.data[cnt].row = row;
Mat.data[cnt].col = col;
Mat.data[cnt].val = val;
cnt++;
}
}
Mat.len = cnt;
}
void transpose1(SparseMatrix Source, SparseMatrix &Dest)
{
Dest.m = Source.n;
Dest.n = Source.m;
Dest.len = Source.len;
int cnt = 0;
for (auto col = 0; col < Source.n; ++col)
{
for (auto i = 0; i < Source.len; ++i)
{
if (col == Source.data[i].col)
{
Dest.data[cnt].row = Source.data[i].col;
Dest.data[cnt].col = Source.data[i].row;
Dest.data[cnt].val = Source.data[i].val;
cnt++;
}
}
}
}
void transpose2(SparseMatrix Source, SparseMatrix &Dest)
{
Dest.len = Source.len;
Dest.m = Source.n;
Dest.n = Source.m;
int cnt[MAX_SIZE] = {0}, pos[MAX_SIZE] = {0};
for (auto i = 0; i < Source.len; ++i)
{
++cnt[Source.data[i].col];
}
pos[0] = 0;
for (auto col = 1; col < Source.n; ++col)
{
pos[col] = pos[col - 1] + cnt[col - 1];
}
for (auto i = 0; i < Source.len; ++i)
{
int col = Source.data[i].col;
Dest.data[pos[col]].row = Source.data[i].col;
Dest.data[pos[col]].col = Source.data[i].row;
Dest.data[pos[col]].val = Source.data[i].val;
++pos[col];
}
}
void showSparseMatrix(SparseMatrix Mat)
{
for (auto i = 0; i < Mat.len; ++i)
{
std::cout << Mat.data[i].row << " " << Mat.data[i].col << " " << Mat.data[i].val << std::endl;
}
}
void SparsetoNormal(SparseMatrix Mat)
{
std::vector<std::vector<int>> temp(Mat.m, std::vector<int>(Mat.n, 0));
for (auto i = 0; i < Mat.len; ++i)
{
temp[Mat.data[i].row][Mat.data[i].col] = Mat.data[i].val;
}
for (auto i = 0; i < Mat.m; ++i)
{
for (auto j = 0; j < Mat.n; ++j)
{
std::cout << temp[i][j] << ' ';
}
std::cout << std::endl;
}
}