HOJ1997//Multiplying Matrices

本文介绍了一个用于计算可相乘矩阵的乘积的算法。通过输入矩阵的维度和元素,程序能够输出乘积矩阵,遵循特定的格式和限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Multiplying Matrices

My Tags   (Edit)
  Source : SCU Programming Contest 2005 Final
  Time limit : 1 sec   Memory limit : 32 M

Submitted : 2012, Accepted : 633

Write a program to compute the product of two matrices A and B that are multipliable in the form of AB.

Input

The first line of input is the number of all the test cases that follow. For each test case, there are three positive integer m, n, p (m, n, p <= 100) on the first line, meaning that A's dimension is m rows by n columns while B's is n rows by p columns. Then comes the data of matrices A and B. Matrix A is represented by m lines of integers ranging in [-1000, 1000], with each line containing n integers separated by a space. A line is corresponding to a row of a matrix. The data format for matrix B is much the same except for its potentially different dimensions. Please refer to the section Sample Input.

Output

For each test case, print the product of A multiplied by B in the form described in the section Input, but do not include any dimension description.

Sample Input

2
2 2 2
1 2
3 4
1 2
3 4
1 2 1
1 0
0
1
Sample Output
7 10
15 22
0


Author:  hewei


/*
  题意:两个矩阵的乘法
  思路:算出来打印就行
  注意每行输出的时候最后没有空格
*/
#include <iostream>
#include<stdio.h>
#include<cmath>
#define MAX 1000
int Matrices1[MAX+1][MAX+1],Matrices2[MAX+1][MAX+1],ANS[MAX+1][MAX+1];
using namespace std;
int main()
{
    int N,n,m,p,i,j,k,sum;
    cin >> N;
    while(N--)
    {
        cin >> n >> m >> p;
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
                cin >> Matrices1[i][j];
        for(i=0; i<m; i++)
            for(j=0; j<p; j++)
                cin >> Matrices2[i][j];
        for (i=0; i<n; i++)
        {
            for (j=0; j<p; j++)
            {
                sum=0;
                for (k=0; k<m; k++)
                {
                    sum+=Matrices1[i][k]*Matrices2[k][j];
                }
               ANS[i][j]=sum;
            }
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<p-1; j++)
            {
                cout << ANS[i][j] << " ";
            }
            cout << ANS[i][p-1];
            cout << endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值