矩阵

点击打开链接


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?' ':'
');
49
            }
50
        }
51
    
52
    return 0;
53
}

 

矩阵乘法的算法实现 [转载]


点击打开链接


记得阅读 (矩阵总结)

点击打开链接








永远渴望,大智若愚(stay hungry, stay foolish)
原文地址:https://www.cnblogs.com/h-hkai/p/7406493.html