2022级统计计算机双学位《程序设计原理与C语言》上机作业

1052. 矩阵乘法

单点时限: 2.0 sec

内存限制: 256 MB

定义函数 multiply 计算两个 $n$ 阶方阵 $A$ 和 $B$ 的乘积 C。$2 \leq n \leq N$,元素类型是 int

//********** Specification of multiply **********
void multiply(int (*A)[N], int (*B)[N], int (*C)[N],int n);
/* PreCondition:
A, B, and C are addresses of three matrices
and n (n<=N) is a positive integer
PostCondition:
C is the product of A and B.
*/

只需按要求写出函数定义,并使用给定的测试程序测试你所定义函数的正确性。

不要改动测试程序。

测试正确后,将测试程序和函数定义一起提交。

/***************************************************************/
/*                                                             */
/*  DON'T MODIFY main function ANYWAY!                         */
/*                                                             */
/***************************************************************/
#include <stdio.h>
#define N 10

//********** Specification of multiply **********
void multiply(int (*A)[N], int (*B)[N], int (*C)[N],int n)
/* PreCondition:
A, B, and C are addresses of three matrices
and n (n<=N) is a positive integer
PostCondition:
C is the product of A and B.
*/
{
    //TODO: your function definition
}
/***************************************************************/

int main()
{
    int A[N][N], B[N][N], C[N][N], n, i, j;
    scanf("%d",&n);
    for (i=0;i<n;i++)
        for (j=0;j<n;j++)
            scanf("%d",&A[i][j]);
    for (i=0;i<n;i++)
        for (j=0;j<n;j++)
            scanf("%d",&B[i][j]);
    /********** multiply is called here **************/
    multiply(A,B,C,n);
    /**************************************************/
    for (i=0;i<n;i++)
        for (j=0;j<n;j++)
            printf("%d%c",C[i][j],j<n-1?' ':'\n');
    return 0;
}

输入格式

第 1 行输入一个整数 $n$ ($2 \leq n \leq N$),接下来 $2 \times n$ 行,每行输入 $n$ 个由一个空格分隔的 int,整数的绝对值不超过 $10^4$。前 $n$ 行为方阵 $A$,后 $n$ 行为方阵 $B$。

输出格式

输出 $n$ 行,分别按序对应方阵 $C$ 的每一行,每行 $n$ 个数 (相互之间由一个空格分隔,每行最后一个数后没有空格)。

样例

Input
3
1 2 3
4 5 6
7 8 9
-1 8 9
10 -20 -30
34 56 -25
Output
121 136 -126
250 268 -264
379 400 -402

提示

计算公式为:

$C_{ij} = \sum_{k=0}^{n-1}A_{ik} \times B_{kj}$