3712. Matrix multiplication

本文介绍了一个简单的矩阵乘法算法实现,该算法能够处理大小为 n*n 的两个矩阵 A 和 B 的相乘运算,并通过示例展示了输入输出格式。文章提供了一段 C++ 代码示例,用于帮助理解矩阵乘法的具体实现。

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

3712. Matrix multiplication

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

Given two n*n matrices, A and B, computes its matrices multiplication C=A∙B.

Input

There are multiples test cases. Each case is:

The first line is an integer n(<=10), meaning the size of the matrix. For the following 2*n lines, each line contains n integers. The first n lines contain the elements in matrix A.

Input is terminated by EOF.

Output

For each test case, output:

The multiplication C= A∙B, there will be n lines and each line contains n integers, separating the integers by a space.

Sample Input

2

1 2

3 4

1 1

1 1

Sample Output

3 3

7 7

Hint

//To process multiple cases, you can use some C++ codes like:
//original codes:
//    cin >> a >> b;
//      cout << a + b << endl;
while(cin >> a){
    cin >> b;
    cout << a + b << endl;
}
     
//or use some C codes like:
//original codes:
//    scanf("%d%d", &a, &b);
//    printf("%d\n", a + b);
while(scanf("%d", &a) != EOF){
    scanf("%d", &b);
    printf("%d\n", a + b);
}

// Problem#: 3712

// Submission#: 1984646

// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/

// All Copyright reserved by Informatic Lab of Sun Yat-sen University

#include<iostream>

#include<stdio.h>

#include<string>

#include<cmath>

using namespace std;

int main(){

    int n,i;

    while(scanf("%d",&n)!=EOF){

        int x,y;

        int **a,**b;

        a=new int*[n];

        b=new int*[n];

        for(x=0;x<n;x++){

            a[x]=new int [n];

            b[x]=new int [n];

        }

        for(x=0;x<n;x++){

            for(y=0;y<n;y++){

                cin>>a[x][y];

            }

        }

        for(x=0;x<n;x++){

            for(y=0;y<n;y++){

                cin>>b[x][y];

            }

        }

        int sum=0,z=0;

        for(x=0;x<n;x++){

            for(y=0;y<n-1;y++){

                for(z=0;z<n;z++){

                    sum+=a[x][z]*b[z][y];

                }

                cout<<sum<<" ";

                sum=0;

            }

                for(z=0;z<n;z++){

                    sum+=a[x][z]*b[z][y];

                }

                cout<<sum<<endl;

                sum=0;

        }

    }

    return 0;

}                   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值