日期:2014-05-20 浏览次数:20859 次
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++) {