Vj数据结构实验5

注意

  1. 数据类型请使用int,本题中所有运算的结果均视作对int型自然溢出
  2. 可以使用 vector 等 STL 中的容器保存稀疏矩阵元素,减少不必要的bug
  3. 各操作需在稀疏矩阵上进行,充分考虑数据的稀疏性, 不得直接或间接转换为二维数组形式计算 ,否则取消成绩

题目描述

  • 创建 稀疏矩阵类 (参照课本 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 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 0 0 0 -1 0 0 -2
0 0 -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 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 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 
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 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3
10 3 1
10 4 1
10 5 1
10 6 3
10 7 2
10 8 1
10 9 1
10 10 3
10 11 3
10 12 3
10 13 2
11 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 

#include <iostream>
#include <iterator>
const int maxsize = 250000;

using namespace std;

struct Matrix // 定义一个三元组元素
{
    int row, col, value;
    Matrix &operator=(Matrix &x) // 重载
    {
        row = x.row;
        col = x.col;
        value = x.value;
        return *this;
    }
};

class sparseMatrix
{
    friend ostream &operator<<(ostream &out, sparseMatrix &a); // 重载 <<
public:
    sparseMatrix(int r, int c);                          // 构造函数
    ~sparseMatrix() { delete[] element; }                // 析构函数
    sparseMatrix &operator=(sparseMatrix &a);            // 重载=
    void add(sparseMatrix &a);                           // 相加函数
    void transpose();                                    // 转置
    void initial(int r, int c);                          // 重置
    void inputAelement(int x, int y, int z);             // 在线性表中插入新的三元组
    void insert(int theIndex, const Matrix &theElement); //在索引为theIndex处插入三元组
    void multiply(sparseMatrix &a)                       // 乘法
    {
        
        if (Cols != a.Rows)
        {
            *this = a;
            cout << -1 << endl;
        }
        else
        {
            sparseMatrix b(Rows, a.Cols); 
            int sizeofrow[10000];
            int nextofrow[10000];
            int theanswer[10000];
            for (int i = 1; i <= a.Rows; i++) 
                sizeofrow[i] = 0;
            for (int i = 0; i < a.Terms; i++)
            {
                if (a.element[i].value != 0)
                {
                    sizeofrow[a.element[i].row]++;
                }
            }
            nextofrow[1] = 0;
            for (int i = 2; i <= a.Terms; i++) 
                nextofrow[i] = nextofrow[i - 1] + sizeofrow[i - 1];
            int p = 0;
            for (int i = 1; i <= Rows && p < Terms; i++)
            {
                for (int j = 1; j <= a.Cols; j++)
                    theanswer[j] = 0;
                while (p < Terms && element[p].row == i) 
                {
                    int t = element[p].col;
                    if (sizeofrow[t] != 0) 
                    {
                        for (int q = nextofrow[t]; q < nextofrow[t] + sizeofrow[t]; q++)
                        { 
                            theanswer[a.element[q].col] += element[p].value * a.element[q].value;
                        }
                    }
                    p++; 
                }
                for (int k = 1; k <= a.Cols; k++) 
                {
                    if (theanswer[k] != 0)
                    {
                        b.element[b.Terms].value = theanswer[k];
                        b.element[b.Terms].row = i;
                        b.element[b.Terms].col = k;
                        b.Terms++;
                    }
                }
            }
            *this = b;
        }
    }
    void output(); // 输出
private:
    int Rows, Cols, Terms;
    Matrix *element;
    int maxSize;
};
// 在线性表中插入新的三元组
void sparseMatrix::inputAelement(int x, int y, int z)
{
    Matrix New;
    New.row = x;
    New.col = y;
    New.value = z;
    element[Terms] = New;
    Terms++;
}
// 转置
void sparseMatrix::transpose()
{
    sparseMatrix b(Cols, Rows);
    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;
    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 (int i = 0; i < Terms; i++)
    {
        int j = rowNext[element[i].col]++;
        b.element[j].row = element[i].col;
        b.element[j].col = element[i].row;
        b.element[j].value = element[i].value;
    }
    *this = b;
}
// 重置
void sparseMatrix::initial(int r, int c)
{
    Rows = r; // 矩阵的行和列
    Cols = c;
    Terms = 0;
    int flag = 0;
    for (int i = 0; i < Rows; i++)
    {
        for (int j = 0; j < Cols; j++)
        {
            int b;
            cin >> b;
            if (b != 0)
            {
                element[flag].row = i + 1;
                element[flag].col = j + 1;
                element[flag].value = b;
                Terms++;
                flag++;
            }
        }
    }
}
// 构造函数
sparseMatrix::sparseMatrix(int r, int c)
{
    element = new Matrix[maxsize];
    Rows = r;
    Cols = c;
    Terms = 0;
}
// 重载=
sparseMatrix &sparseMatrix::operator=(sparseMatrix &a)
{
    Rows = a.Rows;
    Cols = a.Cols;
    Terms = a.Terms;
    maxSize = a.maxSize;
    for (int i = 0; i < Terms; i++)
        element[i] = a.element[i];
    return *this;
}
// 重载<<
ostream &operator<<(ostream &out, sparseMatrix &a)
{
    cout << a.Rows << " " << a.Cols << endl;
    for (int i = 1; i < a.Rows + 1; i++)
    {
        for (int j = 1; j < a.Cols + 1; j++)
        {
            int flag = 0;
            for (int k = 0; k < a.Terms; k++)
            {
                if (a.element[k].row == i && a.element[k].col == j)
                {
                    cout << a.element[k].value << " ";
                    flag++;
                }
            }
            if (flag == 0)
                cout << 0 << " ";
        }
        cout << endl;
    }
    return out;
}
// 输出
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;
    }
}
// 在索引theIndex出插入theElement
void sparseMatrix::insert(int theIndex, const Matrix &theElement)
{
    copy_backward(element + theIndex, element + Terms,
                  element + Terms + 1);

    element[theIndex].col = theElement.col;
    element[theIndex].row = theElement.row;
    element[theIndex].value = theElement.value;

    Terms++;
}
// 相加
void sparseMatrix::add(sparseMatrix &b)
{
    sparseMatrix c(Rows, Cols);
    if (b.Cols != Cols || b.Rows != Rows)
    {
        *this = b;
        cout << -1 << endl;
        return;
    }
    int cSize = 0;
    int it = 0;
    int ib = 0;
    int itEnd = Terms;
    int ibEnd = b.Terms;
    while (it != itEnd && ib != ibEnd)
    {

        int tIndex = element[it].row * Cols + element[it].col;
        int bIndex = b.element[ib].row * Cols + b.element[ib].col;

        if (tIndex < bIndex)
        {
            // c.element[cSize++] = element[it];
            c.insert(cSize++, element[it]);
            it++;
        }
        else
        {
            if (tIndex == bIndex)
            {
                if (element[it].value + b.element[ib].value != 0)
                {
                    Matrix mTerm;
                    mTerm.row = element[it].row;
                    mTerm.col = element[it].col;
                    mTerm.value = element[it].value + b.element[ib].value;
                    // c.element[cSize++] = mTerm;
                    c.insert(cSize++, mTerm);
                }

                it++;
                ib++;
            }
            else
            {
                // c.element[cSize++] = b.element[ib];
                c.insert(cSize++, b.element[ib]);
                ib++;
            }
        }
    }

    for (; it != itEnd; it++)
        //  c.element[cSize++] = element[it];
        c.insert(cSize++, element[it]);
    for (; ib != ibEnd; ib++)
        //   c.element[cSize++] = b.element[ib];
        c.insert(cSize++, b.element[ib]);

    *this = c;
}

int main()
{
    sparseMatrix x(0, 0);
    int w; // 操作次数
    int r, c, t, trans;
    cin >> w;
    for (int i = 0; i < w; i++)
    {
        cin >> trans;
        switch (trans)
        {
        // 重置
        case 1:
            cin >> r >> c;
            x.initial(r, c);
            break;
        // 乘法
        case 2:
        {
            cin >> r >> c >> t;
            sparseMatrix y(r, c);
            for (int i = 0; i < t; i++)
            {
                int r1, c1, t1;
                cin >> r1 >> c1 >> t1;
                y.inputAelement(r1, c1, t1);
            }
            x.multiply(y);
        }
        break;
        // 加法
        case 3:
        {
            cin >> r >> c >> t;
            sparseMatrix y1(r, c);
            for (int i = 0; i < t; i++)
            {
                int r1, c1, t1;
                cin >> r1 >> c1 >> t1;
                y1.inputAelement(r1, c1, t1);
            }
            x.add(y1);
        }
        break;
        // 输出
        case 4:
            x.output();
            break;
        // 转置
        case 5:
            x.transpose();
            break;
        }
    }
    
    return 0;
}

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值