HDU 4920 疏鬆陣列相乘,hdu4920矩陣相乘
暴力啊,不要想太多
T^T g艸太坑爹了
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;inline void rd(int &ret){char c;do { c = getchar();} while(c < '0' || c > '9');ret = c - '0';while((c=getchar()) >= '0' && c <= '9')ret = ret * 10 + ( c - '0' );}inline void ot(int a) //輸出外掛{ if(a>9) ot(a/10); putchar(a%10+'0');}const int MAX_N = 807;int n;int a[MAX_N][MAX_N], b[MAX_N][MAX_N];int c[MAX_N][MAX_N];int main() { while (1 == scanf("%d", &n)) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int x; rd(x); a[i][j] = x % 3; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int x; rd(x); b[i][j] = x % 3; } } memset(c, 0, sizeof(c)); for (int i = 0; i < n; ++i) { for (int k = 0; k < n; ++k) { if (a[i][k] == 0) continue; for (int j = 0; j < n; ++j) { c[i][j] += a[i][k] * b[k][j]; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j == 0) ot(c[i][j]%3);else {putchar(' '); ot(c[i][j]%3);} } puts(""); } } return 0;}
疏鬆陣列的乘法問題有程式,但是我看不懂講解
前面迴圈是將按照矩陣乘法規則將三元組表格儲存體的還原為正常按下標的運算
最後的迴圈是將其乘法結果再放到一個新的三元組表中,其實程式注釋已經比較清楚了
疏鬆陣列相乘的問題
你的完整的程式呢?這個程式碼很亂,風格也不好:-)
int ctemp[N.nu+1];////////這裡能這樣定義嗎????????????
這樣是不對的,要採用動態分配
int *ctemp=new int[N.nu+1];使用完後要用delete []ctemp;釋放記憶體
for(arow=1;arow<=M.mu;++arow)
{ int i;
for(i=1;i<=N.nu;i++)ctemp[i]=0;//////////這啥意思呢???????????
這裡的話表面上看來就是數組初始化,這是很常見的情況,在數組使用前要將其初始化為0,不然可能是一個很大的負數
Q.rpos[arow]=Q.tu+1;///////////還有這裡???????
這裡應該就是乘法的運算部分了,你應該先仔細瞭解一下乘法的運算過程,可以自己獨立的嘗試著寫一個,然後再對照別人的程式就應該比較容易理解了^_^
希望我的回答對你有協助。