hdu 4920 Matrix multiplication(矩陣坑題)

來源:互聯網
上載者:User

標籤:矩陣

http://acm.hdu.edu.cn/showproblem.php?pid=4920


被這道題虐了一下午,啥也不說了。繼續矩陣吧。

逾時就超在每步取餘上,要放在最後取餘,再者注意三個迴圈的次序。


#include <stdio.h>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <cmath>#include <stdio.h>#include <stdlib.h>#include <cstring>#include <iostream>#include <limits.h>#include <algorithm>#define LL long long#define _LL __int64using namespace std;const int maxn = 810;int a[maxn][maxn],b[maxn][maxn];int ans[maxn][maxn];int n;int main(){while(~scanf("%d",&n)){for(int i = 0; i < n; i++)for(int j = 0; j < n; j++){scanf("%d",&a[i][j]);a[i][j] %= 3;}for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){scanf("%d",&b[i][j]);b[i][j] %= 3;}}memset(ans,0,sizeof(ans));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++){ans[i][j] += a[i][k]*b[k][j];}}}for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){printf("%d",ans[i][j]%3);if(j == n-1)printf("\n");elseprintf(" ");}}}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.