51NOD 1137 矩阵乘法

1137 矩阵乘法

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
收藏
关注
给出2个N * N的矩阵M1和M2,输出2个矩阵相乘后的结果。
Input
第1行:1个数N,表示矩阵的大小(2 <= N <= 100)
第2 - N + 1行,每行N个数,对应M1的1行(0 <= M1[i] <= 1000)
第N + 2 - 2N + 1行,每行N个数,对应M2的1行(0 <= M2[i] <= 1000)
Output
输出共N行,每行N个数,对应M1 * M2的结果的一行。
Input示例
2
1 0
0 1
0 1
1 0
Output示例
0 1
1 0

#include <iostream>
#include <algorithm>

using namespace std ; 

#define maxn 200
int node1[maxn][maxn] ; 
int node2[maxn][maxn] ; 
int result[maxn][maxn]; 
int main(){
    int n ; 
    
    while(cin >> n ){
        for(int i = 0 ; i < n ; i ++){
            for(int j = 0 ; j < n ; j ++){
                cin >> node1[i][j] ; 
            }
        }
        for(int i = 0 ; i < n ; i ++){
            for(int j = 0 ; j < n ; j ++){
                cin >> node2[i][j] ; 
            }
        }
        
        for(int i = 0 ; i < n ; i ++ ) {
            for(int j = 0 ; j < n ; j ++){
                result[i][j] = 0 ; 
                for(int k = 0 ; k < n ; k++){
                    result[i][j] += node1[i][k] * node2[k][j] ;  
                }
            }
        }
        
        for(int i = 0 ; i < n ; i ++){
            for(int j = 0 ; j < n ; j ++ ){
                if(j == 0 )
                    cout << result[i][j]  ; 
                else {
                    cout << " " << result[i][j]  ; 
                }
            }
            cout << endl ; 
        }
    }    
    
    return 0 ; 
}

猜你喜欢

转载自www.cnblogs.com/yi-ye-zhi-qiu/p/9289039.html
今日推荐