山东大学数据结构实验五
稀疏矩阵
声明:仅限参考,全文复制会被查重哦
注意
- 数据类型请使用int,本题中所有运算的结果均视作对int型自然溢出
- 可以使用 vector 等 STL 中的容器保存稀疏矩阵元素,减少不必要的bug
- 各操作需在稀疏矩阵上进行,充分考虑数据的稀疏性, 不得直接或间接转换为二维数组形式计算 ,否则取消成绩
题目描述
- 创建 稀疏矩阵类 (参照课本 MatrixTerm 三元组定义) ,采用行主顺序把稀疏矩阵非0元素映射到一维数组中,提供操作:两个稀疏矩阵相加、两个稀疏矩阵相乘、稀疏矩阵的转置、输出矩阵。
- 键盘输入矩阵的行数、列数;并按行优先顺序输入矩阵的各元素值,建立矩阵;
- 对建立的矩阵执行相加、相乘、转置的操作,输出操作的结果矩阵。
操作描述
- 为方便操作描述,我们假设存在一个矩阵 P,下列各操作实际为对矩阵 P 的操作。
- 重置矩阵 :
1
矩阵的行数n 矩阵的列数m
[n行m列 表示矩阵中的所有元素]
即重置矩阵 P 的尺寸为 n 行 m 列,且随后按行优先顺序输入矩阵 P 的各个元素。 - 矩阵乘法:
2
矩阵的行数 矩阵的列数
矩阵中非零元素个数t
[t行 表示矩阵中非零元素]
t 行非零元素已按行优先顺序给出,矩阵中非零元素的表示为x y v
,其中x
表示行序号,y
表示列序号,v
表示非零元素值,行列序号从 1 开始。
设输入的矩阵为 Q,若 PxQ 运算合法,则将 PxQ 的结果矩阵赋给 P,若不合法,则将 Q 赋给 P,同时输出 -1。 - 矩阵加法:
3
矩阵的行数 矩阵的列数
矩阵中非零元素个数t
[t行 表示矩阵中非零元素]
t 行非零元素已按行优先顺序给出,矩阵中非零元素的表示为x y v
,其中x
表示行序号,y
表示列序号,v
表示非零元素值,行列序号从 1 开始。
设输入的矩阵为 Q,若 P+Q 运算合法,则将 P+Q 的结果矩阵赋给 P,若不合法,则将 Q 赋给 P,同时输出 -1。 - 输出操作:
4
设当前矩阵 P 的尺寸为 n 行 m 列,第一行输出矩阵 P 的行数和列数,随后 n 行按行优先顺序输出矩阵 P,每行 m 个数字,来表示当前的矩阵内容,每行数字之间用空格分隔。 - 转置操作:
5
设当前矩阵 P 的尺寸为 n 行 m 列,将其转置为 m 行 n 列的矩阵,无需输出。
格式
输入
第一行一个 w 代表操作个数,接下来若干行是各个操作,其中保证第一个操作一定为重置矩阵。
输出
当执行操作 4 时,输出矩阵 P;当执行操作 2 或 3 时,若对应运算不合法,则输出 -1。
样例1
输入
7
1
5 5
2 1 0 0 0
0 0 -1 0 0
0 0 0 0 0
0 0 -1 0 0
0 0 0 0 0
3
5 5
4
2 2 5
3 5 8
4 4 2
5 3 4
4
2
5 5
3
1 1 8
2 4 4
3 5 2
4
5
4
输出
5 5
2 1 0 0 0
0 5 -1 0 0
0 0 0 0 8
0 0 -1 2 0
0 0 4 0 0
5 5
16 0 0 4 0
0 0 0 20 -2
0 0 0 0 0
0 0 0 0 -2
0 0 0 0 8
5 5
16 0 0 0 0
0 0 0 0 0
0 0 0 0 0
4 20 0 0 0
0 -2 0 -2 8
样例2
输入
40
1
10 20
-1 0 1 0 0 0 0 0 -1 0 0 0 -1 0 -1 0 0 -1 1 -1
0 0 2 -1 0 0 0 0 0 -1 0 0 0 0 0 0 0 1 -2 0
1 0 0 0 0 0 0 0 0 0 -1 -2 -1 0 -1 0 0 0 0 0
0 0 0 1 0 -1 -1 -1 0 0 1 0 0 0 0 0 0 0 -1 0
0 0 0 1 0 0 0 0 0 1 -1 1 0 0 0 0 -1 0 0 0
1 0 -1 1 2 0 0 0 1 0 0 0 0 -1 0 1 -1 1 -1 0
-1 0 0 0 -1 0 0 0 0 0 -1 0 0 -1 2 0 0 -1 0 0
-1 -1 -1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 -3 0 0 0 0 -1 -2 1 0 2 0 -1 -1 0
-1 1 0 1 -1 0 0 0 -1 0 -1 0 0 0 0 1 0 0 -1 1
2
10 20
7
2 16 9
3 7 3
3 17 4
6 3 4
7 12 10
8 13 6
10 8 3
2
10 20
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1 0 0 1 0 0 1 -1 0 0 0 1 0 0 0 0 1
0 0 0 0 1 -1 -1 1 1 0 0 0 0 -1 0 0 0 0 0
0 -1 0 0 0 0 2 0 0 0 0 2 2 0 0 0 0 0 -1
0 0 0 -1 0 -1 0 0 0 0 0 -1 0 0 0 0 0 0 0
0 -1 -1 0 0 1 0 -1 1 0 -1 0 0 0 0 0 0 0 1
4
2
19 19
6
5 5 2
5 17 5
12 3 3
13 15 5
14 3 5
15 9 7
2
19 19
8
7 9 1
10 1 6
12 2 4
14 3 9
14 8 2
16 7 3
18 1 1
18 14 4
2
19 19
9
1 5 3
1 18 10
4 15 4
6 7 9
11 19 6
12 2 1
14 7 6
14 14 2
17 9 8
2
19 19
7
4 18 7
5 9 1
7 2 6
11 9 3
12 16 3
15 9 2
16 5 5
2
19 19
3
3 12 4
17 7 5
18 16 4
5
2
19 19
1
17 17 2
3
19 19
6
11 8 5
11 14 5
12 19 6
17 5 4
17 15 6
19 19 4
2
19 19
7
1 1 4
4 12 5
6 1 9
7 8 3
9 18 8
13 12 2
16 14 2
2
19 19
2
8 11 7
12 4 8
3
19 19
7
1 16 5
3 9 6
5 15 3
14 14 10
15 9 6
15 14 3
15 19 7
2
19 19
6
1 19 2
5 8 6
6 16 6
9 6 6
10 18 9
15 7 5
5
5
2
19 19
6
6 7 1
10 7 6
13 5 5
15 16 6
17 9 10
19 15 3
2
19 19
6
3 5 4
4 9 5
5 15 1
11 3 5
17 5 6
17 7 7
2
19 19
1
14 6 7
5
2
19 19
3
3 10 8
4 18 1
15 15 8
2
19 19
4
5 8 10
6 9 10
6 16 6
14 15 4
5
4
2
19 19
9
2 17 3
4 18 9
12 3 8
13 11 10
13 19 7
14 12 4
15 4 9
17 8 9
19 4 5
2
19 19
1
7 17 6
输出
-1
-1
-1
10 20
0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 10 0 0 0 0 6 0 0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0 0 -1 0 -1 0 0 0 0 0 -1 0 0 0 0 0 0 0
0 -1 -1 0 0 1 0 -1 1 0 -1 0 0 0 0 0 0 0 1
19 19
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
样例3
输入
20
1
5 11
-22324 -8307 9206 122 -7218 21649 -16209 11639 3813 12960 15895
-6355 8061 -4443 9028 -2663 20150 6485 8100 -12939 -1189 -8954
17884 -3031 -10317 6894 9240 -1078 9344 -16194 -1543 -16063 -15494
-19732 3868 -25565 1922 4300 8148 -13256 4611 2077 26163 10738
10610 -2944 6357 4205 -12046 2795 13566 18396 11768 -5985 -3455
2
5 5
25
1 1 1
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1 1
11 2 2
11 3 2
11 4 3
11 5 2
11 6 1
11 7 1
11 8 2
11 9 3
11 10 2
11 11 3
11 12 2
11 13 3
12 1 2
12 2 3
12 3 1
12 4 2
12 5 2
12 6 2
12 7 3
12 8 3
12 9 2
12 10 2
12 11 1
12 12 1
12 13 2
2
5 5
25
1 1 1
1 2 3
1 3 3
1 4 1
1 5 3
2 1 2
2 2 2
2 3 3
2 4 2
2 5 1
3 1 3
3 2 1
3 3 1
3 4 2
3 5 1
4 1 1
4 2 3
4 3 1
4 4 1
4 5 3
5 1 3
5 2 2
5 3 1
5 4 2
5 5 1
2
5 5
25
1 1 2
1 2 1
1 3 2
1 4 1
1 5 2
2 1 1
2 2 1
2 3 2
2 4 2
2 5 2
3 1 1
3 2 1
3 3 1
3 4 3
3 5 3
4 1 2
4 2 1
4 3 3
4 4 3
4 5 3
5 1 2
5 2 1
5 3 2
5 4 1
5 5 1
4
2
5 5
25
1 1 2
1 2 3
1 3 2
1 4 3
1 5 3
2 1 1
2 2 2
2 3 3
2 4 3
2 5 2
3 1 3
3 2 1
3 3 3
3 4 2
3 5 1
4 1 3
4 2 1
4 3 3
4 4 2
4 5 3
5 1 1
5 2 1
5 3 3
5 4 3
5 5 2
4
2
5 5
25
1 1 3
1 2 1
1 3 2
1 4 2
1 5 1
2 1 1
2 2 3
2 3 3
2 4 3
2 5 1
3 1 1
3 2 2
3 3 1
3 4 1
3 5 3
4 1 2
4 2 2
4 3 1
4 4 3
4 5 1
5 1 2
5 2 2
5 3 3
5 4 2
5 5 1
输出
-1
5 5
1 2 2 2 3
1 1 3 2 2
1 1 3 2 1
2 1 3 2 2
2 2 3 2 2
5 5
16143 13975 16499 16583 13958
14052 12162 14360 14438 12152
12440 10759 12704 12777 10751
15373 13308 15713 15794 13293
17168 14856 17540 17632 14838
5 5
1945386 1782533 1254468 1903751 1285027
1693395 1551629 1091979 1657151 1118577
1498468 1373016 966290 1466390 989824
1852660 1697571 1194674 1813009 1223776
2068355 1895207 1333773 2024085 1366260
5 5
1945386 1782533 1254468 1903751 1285027
1693395 1551629 1091979 1657151 1118577
1498468 1373016 966290 1466390 989824
1852660 1697571 1194674 1813009 1223776
2068355 1895207 1333773 2024085 1366260
-1
-1
5 5
16 11 20 22 23
15 10 19 22 24
14 8 17 15 18
14 9 18 16 17
15 9 19 17 20
5 5
192 135 260 234 202
187 130 255 229 198
150 108 202 184 156
156 111 208 188 160
167 119 225 204 173
数据范围

限制
2s, 64MB for each test case。
/* created by LYZ */
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
struct Matrix{
int row, col, value;//留一个判断两个矩阵元组相等的接口 ~~~~
};
class sparseMatrix{//稀疏矩阵的线性结构
public:
sparseMatrix(int r, int c){
rows = r, cols = c, terms = 0;
}
//sparseMatrix( sparseMatrix& b){//复制构造函数
// (*this).rows = b.rows;
// (*this).cols = b.cols;
// (*this).terms = b.terms;
//}
/*~sparseMatrix(){ }*/
void add(sparseMatrix &b);
void transpose();
void initialize(int r, int c);
void insert(int index, Matrix b);
void output();
void input(int x, int y, int z);
void multiply(sparseMatrix &a);
private:
int rows, cols, terms;//rows是原始矩阵的行数,cols是原始矩阵的列数
// terms是非零元素的个数
vector<Matrix> element;//三元组,表示非零元在矩阵中的行、列、以及取值
};
void sparseMatrix::initialize(int r, int c){//初始化一个稀疏矩阵
sparseMatrix d(r,c);
rows = r, cols = c, terms = 0;
int count = r*c;
int currentElement;
for (int i = 0; i < count; i++){
cin >> currentElement;
if (currentElement != 0){
Matrix newMatrix;
newMatrix.row = i / c + 1;
newMatrix.col = i % c + 1;
newMatrix.value = currentElement;
d.terms++;
d.element.push_back(newMatrix);
}
}
*this = d;
}
void sparseMatrix::multiply(sparseMatrix &a) // 乘法
{
if (cols != a.rows) {
*this = a;
cout << -1 << endl;
return;
} else {
sparseMatrix b(rows, a.cols);
int rowsize[10000]={0};
int rownext[10000]={0};
int answer[10000]={0};
for (int i = 0; i < a.terms; i++) {
rowsize[a.element[i].row]++;
}
for (int i = 2; i <= a.terms; i++)
rownext[i] = rownext[i - 1] + rowsize[i - 1];
int p = 0;
for (int i = 1; i <= rows && p < terms; i++) {
for (int j = 1; j <= a.cols; j++)
answer[j] = 0;
while (p < terms && element[p].row == i) {
int t = element[p].col;
if (rowsize[t] != 0) {
for (int q = rownext[t]; q < rownext[t] + rowsize[t]; q++) {
answer[a.element[q].col] += element[p].value * a.element[q].value;
}
}
p++;
}
for (int k = 1; k <= a.cols; k++) {
if (answer[k] != 0) {
Matrix tapleMatrix;
tapleMatrix.value=answer[k];
tapleMatrix.row=i;
tapleMatrix.col=k;
b.element.push_back(tapleMatrix);
b.terms++;
}
}
}
*this = b;
}
}
void sparseMatrix::insert(int index, Matrix b)
{
element.insert(element.begin() + index , b);
terms++;
}
void sparseMatrix::add(sparseMatrix &b){
if (b.rows != rows || b.cols != cols)
{
*this = b;
cout << "-1" << endl;
return;
}
int sizeofc = 0;
vector<Matrix>::iterator i1 = element.begin();
vector<Matrix>::iterator i2 = element.end();
vector<Matrix>::iterator i3 = b.element.begin();
vector<Matrix>::iterator i4 = b.element.end();
sparseMatrix c(rows, cols);
while (i1 != i2 && i3 != i4)
{
int index1 = (*i1).row * cols + (*i1).col;
int index2 = (*i3).row * cols + (*i3).col;
// cout << index1 << " " << index2 << " ";
if (index1 < index2){
c.insert(sizeofc, (*i1));
sizeofc++;
i1++;
}
else {
if (index1 == index2){
if ((*i1).value + (*i3).value != 0){
Matrix newMatrix;
newMatrix.col = (*i1).col;
newMatrix.row = (*i1).row;
newMatrix.value = (*i1).value + (*i3).value;
c.insert(sizeofc, newMatrix);
sizeofc++;
}
i1++, i3++;
}
else {
c.insert(sizeofc, (*i3));
sizeofc++;
i3++;
}
}
}
for (; i1 != i2; i1++)
{
c.insert(sizeofc, (*i1));
sizeofc++;
}
for (; i3 != i4; i3++)
{
c.insert(sizeofc, (*i3));
sizeofc++;
}
*this = c;
}
void sparseMatrix::transpose()
{
Matrix flagmatrix;
flagmatrix.col=0;
flagmatrix.row=0;
flagmatrix.value=0;
sparseMatrix b(cols, rows);
for(int i =0 ;i<terms;i++)
b.element.push_back(flagmatrix);
b.terms = terms;
b.cols = rows;
b.rows = cols;
int *colSize = new int[cols + 1]; // 第i列的非0元素个数
int *rowNext = new int[cols + 1]; // 第i行首个非0元素在b中的索引
// 初始化
for (int i = 1; i <= cols; i++)
colSize[i] = 0;
vector<Matrix>::iterator i1 = element.begin();
vector<Matrix>::iterator i2 = element.end();
for (; i1 != i2; i1++)
colSize[(*i1).col]++;//8
//for (int i = 0; i < terms; i++)
// colSize[element[i].col]++;
// 寻找b中每一行的起始点
rowNext[1] = 0;
for (int i = 2; i <= cols; i++)
rowNext[i] = rowNext[i - 1] + colSize[i - 1];
// 实施从*this到b的转置复制
for (vector<Matrix>::iterator i3 = element.begin(); i3 != i2; i3++){
int j = rowNext[(*i3).col]++;
b.element[j].row = (*i3).col;
b.element[j].col = (*i3).row;
b.element[j].value = (*i3).value;
}
*this = b;
}
void sparseMatrix::input(int x, int y, int z)
{
Matrix New;
New.row = x;
New.col = y;
New.value = z;
element.push_back(New);
terms++;
}
void sparseMatrix::output()
{
int i, j, k = 0;
cout << rows << " " << cols << endl;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (k < terms && element[k].row == i + 1 && element[k].col == j + 1)
{
cout << element[k].value << " ";
k++;
}
else
{
cout << 0 << " ";
}
}
cout << endl;
}
}
int main(){
sparseMatrix x(0, 0);
int w; // 操作次数
int row, col, terms, flag;
cin >> w;
for (int i = 0; i < w; i++)
{
cin >> flag;
switch (flag)
{
// 重置
case 1:
cin >> row >> col;
x.initialize(row, col);
break;
// 乘法
case 2:
{
cin >> row >> col >> terms;
sparseMatrix y(row, col);
for (int i = 0; i < terms; i++)
{
int r1, c1, t1;
cin >> r1 >> c1 >> t1;
y.input(r1, c1, t1);
}
x.multiply(y);
}
break;
// 加法
case 3:
{
cin >> row >> col >> terms;
sparseMatrix y1(row, col);
for (int i = 0; i < terms; i++)
{
int r1, c1, t1;
cin >> r1 >> c1 >> t1;
y1.input(r1, c1, t1);
}
x.add(y1);
}
break;
// 输出
case 4:
x.output();
break;
// 转置
case 5:
x.transpose();
break;
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· Apache Tomcat RCE漏洞复现(CVE-2025-24813)