【问题描述】
矩阵乘法的计算方法定义为:
对于矩阵A[m][q]*B[q][n],
相乘的结果为矩阵C[m][n]且对于矩阵C中每一项都有
C[i][j] = A[i][0]*B[0][j] + A[i][1]*B[1][j] + ..... + A[i][q]*B[q][j]
注意只有当前一矩阵A的列数等于后一矩阵B的行数时两个矩阵才能相乘。
【输入形式】
两个矩阵A[x1*y1]和B[x2*y2]
第一行为矩阵的大小,后面跟着输入矩阵,所有元素都是整数,矩阵的行和列大小不超过100
x1 y1
a00 a01 a02
a10 a11 a12
a20 a21 a22
a30 a31 a32
x2 y2
b00 b01 b02 b03 b04
b10 b11 b12 b13 b14
b20 b21 b22 b23 b24 
【输出形式】
c00 c01 c02 c03 c04
c10 c11 c12 c13 c14
c20 c21 c22 c23 c24
c30 c31 c32 c33 c34
【样例输入】
4 3
1 2 3
4 5 6
7 8 9
10 11 12
3 5
7 8 9 10 11
4 5 6 7 8
1 2 3 4 5
【样例输出】
18  24  30  36  42
54  69  84  99 114
90 114 138 162 186
126 159 192 225 258
【样例说明】
【评分标准】20分

解决方案 »

  1.   

    package java8.exam;
    import java.util.Scanner;public class Jayz {
    public static void main(String[] args) throws java.io.IOException{
            Scanner input = new Scanner(System.in);
            int rowsA = input.nextInt();
            int columnsA = input.nextInt();
            int[][] a = new int[rowsA][columnsA];
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    a[i][j] = input.nextInt();
                }
            int rowsB = input.nextInt();
            int columnsB = input.nextInt();
            int[][] b = new int[rowsB][columnsB];
            for ( i=0 ; i < b.length; i++) {
                for (int j = 0; j < b[0].length; j++) {
                    b[i][j] = input.nextInt();
                    }
            int[][] c = new int[rowsA][columnsB];
             c = multiplyMatrix(a, b);
             String output = "";
            for (i = 0; i < c.length; i++) {
                for (int j = 0; j < c[0].length; j++) {
                    if (c[i][j] >= 1 && c[i][j] < 10) {
                        output += c[i][j] + " ";
                    }
                    else if (c[i][j] >= 10 && c[i][j] < 100) {
                        output += c[i][j] + " ";
                    }
                    else {
                        output += c[i][j] + " ";
                    }
                }            output += '\n';
            }        System.out.println(output);
        }        }
            }
            
    public static int[][] multiplyMatrix(int[][] a, int[][] b) {
           int[][] c = new int[a.length][b[0].length];
           for (int i = 0; i < c.length; i++) {
                for (int j = 0; j < c[0].length; j++) {
                    c[i][j] = 0;
                }
            }       for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < b[0].length; j++) {
                    for (int k = 0; k < a[0].length; k++) {
                        c[i][j] += a[i][k] * b[k][j];
                    }
                }
            }        return c;
        }



    }

      

  2.   

    到底哪里有问题啊,输入完数组A不能输入数组B,本人刚学java,无知错误勿喷
      

  3.   

    ……格式化一下就看出问题在哪了。从括号没配错这点来看应该不是笔误……
    那就是LZ连程序逻辑都没整理好。建议LZ写代码前画下流程图。(不要觉得画流程图丢面子,大家都要做这项工作的,只是熟练的程序员可以自行在脑内完成简单的流程图。没到这程度的时候就老实动手画吧)package com.codetest.test;import java.util.Scanner;public class MatrixTest {
    public static void main(String[] args) throws java.io.IOException {
    Scanner input = new Scanner(System.in);
    int rowsA = input.nextInt();
    int columnsA = input.nextInt();
    int[][] a = new int[rowsA][columnsA];
    for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[0].length; j++) {
    a[i][j] = input.nextInt();
    }
    int rowsB = input.nextInt();
    int columnsB = input.nextInt();
    int[][] b = new int[rowsB][columnsB];
    for (i = 0; i < b.length; i++) {
    for (int j = 0; j < b[0].length; j++) {
    b[i][j] = input.nextInt();
    }
    int[][] c = new int[rowsA][columnsB];
    c = multiplyMatrix(a, b);
    String output = "";
    for (i = 0; i < c.length; i++) {
    for (int j = 0; j < c[0].length; j++) {
    if (c[i][j] >= 1 && c[i][j] < 10) {
    output += c[i][j] + " ";
    } else if (c[i][j] >= 10 && c[i][j] < 100) {
    output += c[i][j] + " ";
    } else {
    output += c[i][j] + " ";
    }
    } output += '\n';
    } System.out.println(output);
    } }
    } public static int[][] multiplyMatrix(int[][] a, int[][] b) {
    int[][] c = new int[a.length][b[0].length];
    for (int i = 0; i < c.length; i++) {
    for (int j = 0; j < c[0].length; j++) {
    c[i][j] = 0;
    }
    } for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < b[0].length; j++) {
    for (int k = 0; k < a[0].length; k++) {
    c[i][j] += a[i][k] * b[k][j];
    }
    }
    } return c;
    }
    }
      

  4.   

    改了一下,顺便作了些优化(优化部分LZ可无视,在脑子里形成个印象就好,刚学时不要去想怎样写效率高,只要有这个意识就行了)。另外给LZ提两点建议:
    1、要写注释;
    2、对于要输入的值,要相应的给出提示,不然数据量大了自己在输什么都不知道了。package com.codetest.test;import java.util.Scanner;public class MatrixTest {
    public static void main(String[] args) {
    try {
    Scanner input = new Scanner(System.in); // 输入数组A
    System.out.print("数组A行数与列数:");
    int rowsA = input.nextInt();
    int columnsA = input.nextInt();
    int[][] a = new int[rowsA][columnsA];
    System.out.println("数组A数据:");
    for (int i = 0, l = a.length; i < l; i++) { // 用一临时变量存储数组长度可减少循环节中数组的访问次数,直接用rowsA和columnsA更好(下同)
    for (int j = 0, l1 = a[i].length; j < l1; j++) {
    a[i][j] = input.nextInt();
    }
    } // 输入数组B
    //int rowsB = input.nextInt();
    System.out.print("数组B列数:");
    int columnsB = input.nextInt();
    int[][] b = new int[columnsA][columnsB];
    System.out.println("数组B数据:");
    for (int i = 0, l = b.length; i < l; i++) {
    for (int j = 0, l1 = b[i].length; j < l1; j++) {
    b[i][j] = input.nextInt();
    }
    } // int[][] c = new int[rowsA][columnsB];//不要在这里初始化c,它会在multiplyMatrix函数中完成初始化
    int[][] c = multiplyMatrix(a, b); // String output = ""; //不要在循环节里拼接字符串,会创建大量的StringBuilder对象造成资源浪费
    StringBuilder output = new StringBuilder();
    for (int i = 0, l = c.length; i < l; i++) {
    for (int j = 0, l1 = c[i].length; j < l1; j++) {
    // if (c[i][j] >= 1 && c[i][j] < 10) {
    // output += c[i][j] + " ";
    // } else if (c[i][j] >= 10 && c[i][j] < 100) {
    // output += c[i][j] + " ";
    // } else {
    // output += c[i][j] + " ";
    // }
    output.append(String.format("%-8d ", c[i][j]));
    } output.append('\n');
    } // 输出
    System.out.println(output);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    } // 矩阵乘法
    public static int[][] multiplyMatrix(int[][] a, int[][] b) {
    int[][] c = new int[a.length][b[0].length]; for (int i = 0, l = c.length; i < l; i++) {
    for (int j = 0, l1 = c[i].length; j < l1; j++) {
    c[i][j] = 0;
    }
    } for (int i = 0, l = a.length; i < l; i++) {
    for (int j = 0, l1 = b[0].length; j < l1; j++) {
    for (int k = 0, l2 = a[0].length; k < l2; k++) {
    c[i][j] += a[i][k] * b[k][j];
    }
    }
    } return c;
    }
    }