题解 | #矩阵乘法#

矩阵乘法

http://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        while (sc.hasNext()){
            Integer x = Integer.parseInt(sc.nextLine());
            Integer yx = Integer.parseInt(sc.nextLine());
            Integer y = Integer.parseInt(sc.nextLine());
            int[][] left= new int[x][yx];
            int[][] right= new int[yx][y];
            int[][] result = new int[x][y];
            for(int i=0;i<x;i++){
                String sb = sc.nextLine();
                String[] array= sb.split(" ");
                for (int j = 0; j < array.length; j++) {
                    left[i][j] =Integer.parseInt(array[j]);
                }
            }
            for(int i=0;i<yx;i++){
                String sb = sc.nextLine();
                String[] array= sb.split(" ");
                for (int j = 0; j < array.length; j++) {
                    right[i][j] =Integer.parseInt(array[j]);
                }
            }
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < y; j++) {
                    int sum=0;
                    for (int k = 0; k < yx; k++) {
                        sum = sum +left[i][k]*right[k][j];
                    }
                    result[i][j]=sum;
                }
            }
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < y; j++) {
                    System.out.print(result[i][j] + " ");
                }
                System.out.println();
            }
        }


    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务