西北工业大学数据结构noj2-1

2-1稀疏矩阵转置

question

由于显示问题,输入示例的问号是空格

完整代码

// 稀疏矩阵转置
#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; // m为行数,n为列数
};

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);
    // for (auto i = 0; i < T1.len; ++i)
    // {
    //     std::cout << T1.data[i].row << " " << T1.data[i].col << " " << T1.data[i].val << std::endl;
    // }

    std::cout << "T2--------------------------------------" << std::endl;
    showSparseMatrix(T2);
    // for (auto i = 0; i < T2.len; ++i)
    // {
    //     std::cout << T2.data[i].row << " " << T2.data[i].col << " " << T2.data[i].val << std::endl;
    // }

    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;
    }
}

// 4 4
// 1 1 1
// 2 1 2
// 3 2 3
// 0 0 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值