朴素矩阵乘法
package temp;
import java.util.Arrays;
import java.util.Scanner;
public class temp {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("please input matrix's row:");
int row = input.nextInt();
System.out.println("please input matrix's column:");
int column = input.nextInt();
System.out.println("please input a matrix:");
int matrix[][] = new int[row][column];
for(int i = 0 ; i < row ; i++){
for(int j = 0 ; j < column ; j++){
matrix[i][j] = input.nextInt();
}
}
System.out.println("please input matrix's row:");
int row1 = input.nextInt();
System.out.println("please input matrix's column:");
int column1 = input.nextInt();
System.out.println("please input a matrix:");
int matrix1[][] = new int[row1][column1];
for(int i = 0 ; i < row1 ; i++){
for(int j = 0 ; j < column1 ; j++){
matrix1[i][j] = input.nextInt();
}
}
if(column != row1){
System.out.println("input error");
return;
}
int ans[][] = new int[row][column1];
for(int i = 0 ; i < row ; i ++){
for(int j = 0 ; j < column1 ; j ++){
for(int k = 0 ; k < column ; k ++){
ans[i][j] = ans[i][j] + matrix[i][k] * matrix1[k][j];
}
}
}
//输出
System.out.println("output:");
for(int i = 0 ; i < row ; i++){
for(int j = 0 ; j < column1 ; j++){
System.out.print(ans[i][j] + " ");
}
System.out.println();
}
}
}
版权声明:本文为weixin_46112487原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。