数据结构与算法(C++版本)

首先我们来编写一些矩阵计算的C++实现代码

矩阵相加

#include<iostream>
using namespace std;
const int ROWS = 3;
const int COLS = 3;
void MatrixAdd(int*,int*,int*,int,int);
int main()
{
    int A[ROWS][COLS] = {{1,3,5},{7,9,11},{13,15,17}};
    int B[ROWS][COLS] = {{9,8,7},{6,5,4},{3,2,1}};
    int C[ROWS][COLS] = {0};
    cout << "[矩阵A的各个元素]" << endl;
    for(int i = 0;i < ROWS;i++)
    {
        for(int j = 0;j < COLS;j++)
            cout << A[i][j] << "\t";
        cout << endl;
    }
    cout << "[矩阵B的各个元素]" << endl;
    for(int i = 0;i < ROWS;i++)
    {
        for(int j = 0;j < COLS;j++)
            cout << B[i][j] << "\t";
        cout << endl;
    }
    MatrixAdd(&A[0][0],&B[0][0],&C[0][0],ROWS,COLS);
    cout << "[显示矩阵A和矩阵B相加的结果]" << endl;
    for(int i = 0;i < ROWS;i++)
    {
        for(int j = 0;j < COLS;j++)
            cout << C[i][j] << "\t";
        cout << endl;
    }
    system("pause");
}
void MatrixAdd(int* arrA,int* arrB,int* arrC,int dimX,int dimY)
{
    if(dimX <= 0 || dimY <= 0)
    {
        cout << "矩阵维数必须大于0" << endl;
        return;
    }
    for(int row = 1;row <= dimX;row++)
        for(int col = 1;col <= dimY; col++)
            arrC[(row-1)*dimY+(col-1)] = arrA[(row-1)*dimY + (col-1)] + arrB[(row-1)*dimY + (col-1)];
}

矩阵相乘

#include<iostream>
using namespace std;
void MatrixMultiply(int*,int*,int*,int,int,int);
int main()
{
    int M,N,P;
    int i,j;
    cout << "请输入矩阵A的维数(M,N):" << endl;
    cout << "M= ";
    cin >> M;
    cout << "N= ";
    cin >> N;
    int *A = new int[M*N];
    cout << "[请输入矩阵A的各个元素]" << endl;
    for(i = 0;i < M;i++)
        for(j = 0;j < N;j++)
        {
            cout << "a" << i << j << "=";
            cin >> A[i*N+j];
        }
    cout << "请输入矩阵B的维数(N,P):" << endl;
    cout << "N= ";
    cin >> N;
    cout << "P= ";
    cin >> P;
    int *B = new int[N*P];
    cout << "[请输入矩阵B的各个元素]" << endl;
    for(i = 0;i < N;i++)
        for(j = 0;j < P;j++)
        {
            cout << "b" << i << j << "=";
            cin >> B[i*P+j];
        }
    int *C = new int [M*P];
    MatrixMultiply(A,B,C,M,N,P);
    cout << "[A×B的结果是]" << endl;
    for(i = 0;i < M;i++)
    {
        for(j = 0;j < P;j++)
            cout << C[i*P+j] << "\t";
        cout << endl;
    }
    system("pause");
}
void MatrixMultiply(int* arrA,int* arrB,int* arrC,int M,int N,int P)
{
    if(M <= 0 || N <= 0 || P<= 0)
    {
        cout << "[错误:维数M,N,P必须大于0]" << endl;
        return;
    }
    for(int i = 0;i < M;i++)
        for(int j = 0;j < P;j++)
        {
            int Temp;
            Temp = 0;
            for(int k = 0;k < N;k++)
                Temp = Temp + arrA[i*N+k]*arrB[k*P+j];
            arrC[i*P+j] = Temp;
        }
}
View Code

转置矩阵

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int M,N,row,col;
 6     cout << "[输入M×N矩阵的维数]" << endl;
 7     cout << "请输入维数M: ";
 8     cin >> M;
 9     cout << "请输入维数N: ";
10     cin >> N;
11     int *arrA = new int[M*N];//声明动态数组
12     int *arrB = new int[M*N];
13     cout << "[请输入矩阵内容]" << endl;
14     for(row = 1;row <= M; row++)
15     {
16         for(col = 1;col <= N;col++)
17         {
18             cout << "a" << row << col << "=";
19             cin >> arrA[(row-1)*N + (col-1)];
20         }
21     }
22     cout << "[输入矩阵内容为]" << endl;
23     for(row = 1;row <= M; row++)
24     {
25         for(col = 1;col <= N;col++)
26         {
27             cout << arrA[(row-1)*N + (col-1)] << "\t";
28         }
29         cout << endl;
30     }
31     //进行矩阵转置
32     for(row = 1;row <= N;row++)
33         for(col = 1;col <= M;col++)
34             arrB[(col-1)*N + (row-1)] = arrA[(row-1) + (col-1)*N];
35     cout << "[转置矩阵内容为]" << endl;
36     for(row = 1;row <= N;row++)
37     {
38         for(col=1;col<=M;col++)
39         {
40             cout << arrB[(col-1)*N + (row-1)] << "\t";
41         }
42         cout << endl;
43     }
44     system("pause");
45 }
View Code

稀疏矩阵

一个矩阵中大部分元素为0即可称为稀疏矩阵(Sparse Matrix)

如若使用传统的数组来储存稀疏矩阵也是可以,但是很多元素是0,这样的话会造成内存空间浪费。

因此可以采用三项式的数据结构,如果一个稀疏矩阵有n个非零项目,那么可以利用一个A(0:n,1:3)的二维数组来表示,其中A(0,1)代表稀疏矩阵的列数,A(0,2)代表稀疏矩阵的行数而A(0,3)则是此稀疏矩阵非零项目的总数。每一个非零项目用(i,j,item-value)的形式来表示,其中i为此非零项目所在列数,j为非零项目所在行数,item-value则为此非零项的值。

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
const int _ROWS = 8;
const int _COLS = 9;
const int _NOTZERO = 8;
int main()
{
    int i,j,tmpRW,tmpCL,tmpNZ;
    int temp = 1;
    int Sparse[_ROWS][_COLS];
    int Compress[_NOTZERO][3];
    srand(time(NULL));
    for(i = 0;i < _ROWS;i++)
        for(j = 0;j < _COLS;j++)
            Sparse[i][j] = 0;
    tmpNZ = _NOTZERO;
    for(i = 1; i<tmpNZ+1;i++)
    {
        tmpRW = rand()%_ROWS;
        tmpCL = rand()%_COLS;
        if(Sparse[tmpRW][tmpCL] != 0)
            tmpNZ++;
        Sparse[tmpRW][tmpCL] = i;
    }
    cout << "[稀疏矩阵的各个元素]" << endl;
    for(i = 0;i < _ROWS;i++)
    {
        for(j = 0;j < _COLS;j++)
            cout << "[" << Sparse[i][j] << "]";
        cout << endl;
    }
    Compress[0][0] = _ROWS;
    Compress[0][1] = _COLS;
    Compress[0][2] = _NOTZERO;
    for(i = 0;i < _ROWS;i++)
        for(j = 0;j < _COLS;j++)
            if(Sparse[i][j] != 0)
            {
                Compress[temp][0] = i;
                Compress[temp][1] = j;
                Compress[temp][2] = Sparse[i][j];
                temp++;
            }
    cout << "[稀疏矩阵压缩后的内容]" << endl;
    for(i = 0;i < _NOTZERO+1;i++)
    {
        for(j = 0;j < 3;j++)
            cout << "[" << Compress[i][j] << "]";
        cout << endl;
    }
    system("pause");
}
View Code

 

转载于:https://www.cnblogs.com/Lited/p/4320202.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值