1492.稀疏矩陣的乘法
時限:1000ms 記憶體限制:10000k 總時限:3000ms
描述計算兩個稀疏矩陣的乘法
輸入首先輸入第乙個矩陣的行數和列數,再輸入該矩陣的三元組形式,以0 0 0結束
然後輸入第二個矩陣的行數和列數,再輸入該矩陣的三元組形式,以0 0 0結束
輸出兩個矩陣相乘後得到的矩陣的三元組形式
輸入樣例
3 3
1 1 1
2 2 2
2 3 4
3 1 -4
0 0 0
3 31 3 -2
2 3 -5
3 1 8
3 2 -6
0 0 0
輸出樣例
1 3 -2
2 1 32
2 2 -24
2 3 -10
3 3 8
# include # include # include # include # include # include # include # include using namespace std;
# define elemtype int
# define maxsize 1000
//typedef struct triple
//triple, * ptriple;
//typedef struct
//trimatrix, * ptrimatrix;
typedef struct olnode
olnode, * olink;//建立乙個olink型別的指向這種結構體的指標
typedef struct
; olink chead[maxsize]= ;//先給指標初始化為null否則後面處理起來很麻煩
int mu, nu, tu;//行數 列數 和非零元素數量
} crosslist;
void makerow(int i, int j, olink p, crosslist* pcs);
void insertl(int a, int b, int c, crosslist* pcs);
void makecol(int i, int j, olink p, crosslist* pcs);
void outputl0(crosslist* pcs);
void outputl1(crosslist* pcs);
void makematrix(int x, int y, crosslist* pcs);
void cal(crosslist* pcs1, crosslist* pcs2, crosslist* pcs3, int x, int y);
int main()
cin >> z >> u;
makematrix(z+10, u+10, pcs2);//生成0元 否則很難處理越界的問題
while (1)
makematrix(y+10,z+10,pcs3);
//outputl(pcs1);
//outputl(pcs2);
cal(pcs1,pcs2,pcs3,x,y);
outputl1(pcs3);
return 0;
}void insertl(int a, int b, int c, crosslist* pcs)
//邊界條件 為了防止在沒有right的情況下還向後移動引起段錯誤
if (ka->right != null)
else
}if (flag>0)//在已有元素上加上新值
else//新建立乙個節點 }
void makerow(int a, int b, olink p, crosslist* pcs)
else }
}void makecol(int a, int b, olink p, crosslist* pcs)
else
else break;
} p->down = q->down;
q->down = p;
} //完成列插入
}void outputl0(crosslist* pcs)
else
}u++; }}
void outputl1(crosslist* pcs)
else
if (p->right)p = p->right;//p向右移動
else break;
}} u++; }}
void cal(crosslist* pcs1, crosslist* pcs2, crosslist* pcs3,int x, int y)
else break;
}p3->data = sum;//計算完畢後把結果放到m3合適的地方
p3 = p3->right;//m3上的指標向右移動
u++;
} t++; }}
void makematrix(int x, int y, crosslist* pcs)
} }}
稀疏矩陣的乘法操作
此操作的演算法我就不多說了,裡面敘述得很清楚了。下面就是此程式 cpp view plain copy include include define null 0 define ok 1 define error 0 define maxsize 100 矩陣中非零元的最大值 define maxr...
7 稀疏矩陣的乘法運算
資料壓縮是提高傳輸 儲存效率一種技術。教材第5章介紹了兩種簡單的壓縮儲存方法。本實驗要求實現兩個稀疏矩陣相乘積的演算法。其中稀疏矩陣非零元素數量小於100.輸入 第1個 稀疏矩陣的行數 列數 非零元個數 三個數都大於0 三元組 第2個 稀疏矩陣的行數 列數 非零元個數 三個數都大於0 三元組 以行為...
LeetCode 311 稀疏矩陣的乘法
本題是 leetcode 會員才能看 給你兩個 稀疏矩陣 a 和 b,請你返回 ab 的結果。你可以預設 a 的列數等於 b 的行數。請仔細閱讀下面的示例。示例 輸入 a 1,0,0 1,0 3 b 7 0,0 0 0,0 0 0,1 輸出 10 0 7 00 700 ab 103 x 00 0 7...