矩陣

點擊打開鏈接


ZZNU - 1715

Time Limit: 1000MS   Memory Limit: 524288KB   64bit IO Format: %lld & %lld

 Status

Description

輸入兩個矩陣,分別是m*s,s*n大小。輸出兩個矩陣相乘的結果。

Input

 第一行,空格隔開的三個正整數m,s,n(均不超過200)。
  接下來m行,每行s個空格隔開的整數,表示矩陣A(i,j)。
  接下來s行,每行n個空格隔開的整數,表示矩陣B(i,j)。

Output

m行,每行n個空格隔開的整數,輸出相乘後的矩陣C(i,j)的值。

Sample Input

2 3 2
1 0 -1
1 1 -3
0 3
1 2
3 1

Sample Output

-3 2
-8 2

Hint

矩陣C應該是m行n列,其中C(i,j)等於矩陣A第i行行向量與矩陣B第j列列向量的內積。
例如樣例中C(1,1)=(1,0,-1)*(0,1,3) = 1 * 0 +0*1+(-1)*3=-3

Source

藍橋杯算法訓練

 Status



1
#include<stdio.h>
2
#include<string.h>
3
#include<algorithm>
4
5
using namespace std;
6
7
int a[300][300];
8
int b[300][300];

10
int d[300][300];
11
int main()
12
{
13
    int m,s,n;
14
    int count;
15
    scanf("%d %d %d",&m,&s,&n);
16
    for(int i=0; i<m ; i++)
17
    {
18
        for(int j=0; j<s; j++)
19
        {
20
            scanf("%d",&a[i][j]);
21
        
22
        }
23
    }
24
        for(int w=0; w<s; w++)
25
        {
26
                for(int k=0; k<n; k++)
27
                {
28
                    scanf("%d",&b[w][k]);
29
                }
30
        }
31
                for(int x=0; x<m; x++)
32
                {
33
                    for(int y=0; y<n; y++)
34
                    {
35
                        
36
                        for(int t=0; t<s; t++)			//這個地方是關鍵
37
                        {
38
                            d[x][y]+=a[x][t]*b[t][y];                           
39
                        }
40
41
                    }
42
                }
43
        for(int x=0; x<m; x++)
44
        {
45
            for(int y=0; y<n; y++)
46
            {
47
                printf("%d",d[x][y]);
48
                printf("%c",y<n-1?' ':'\n');
49
            }
50
        }
51
    
52
    return 0;
53
}

 

矩陣乘法的算法實現 [轉載]


點擊打開鏈接


記得閱讀 (矩陣總結)

點擊打開鏈接








發佈了74 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章