HDU 4920 疏鬆陣列相乘,hdu4920矩陣相乘

來源:互聯網
上載者:User

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;///////////還有這裡???????
這裡應該就是乘法的運算部分了,你應該先仔細瞭解一下乘法的運算過程,可以自己獨立的嘗試著寫一個,然後再對照別人的程式就應該比較容易理解了^_^
希望我的回答對你有協助。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.