public class Main {
public static void main(String[] args){
//Check the minimum and maximum values of int type
System.out.println("int minimum/Maximum value= " + Integer.MIN_VALUE+" ~ "+Integer.MAX_VALUE);
System.out.println("byte minimum/Maximum value= " + Byte.MIN_VALUE+" ~ "+Byte.MAX_VALUE);
System.out.println("short minimum/Maximum value= " + Short.MIN_VALUE+" ~ "+Short.MAX_VALUE);
System.out.println("long minimum/Maximum value= " + Long.MIN_VALUE+" ~ "+Long.MAX_VALUE);
}
}
int minimum/Maximum value= -2147483648 ~ 2147483647
byte minimum/Maximum value= -128 ~ 127
short minimum/Maximum value= -32768 ~ 32767
long minimum/Maximum value= -9223372036854775808 ~ 9223372036854775807
Create a program that inputs the n × m matrix A and the m × l matrix B, and outputs the product of them, the n × l matrix C. Here, let each element of A, B, and C be aij, bij, and cij, respectively. Input N, m, l are given on the first line, separated by spaces. The following rows are given the n × m matrix A and the m × l matrix B. Output Output the n × l matrix C element cij. Separate adjacent elements on each line with a single space. Constraints
//Inner product calculation
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
int l=scan.nextInt();
//0≤aij,bij ≤ 10000
//Note that it may not fit in the maximum range of int type 2147483647!
// int[][] a=new int[n][m];
// int[][] b=new int[m][l];
// int[][] c=new int[n][l];
long[][] a = new long[n][m];
long[][] b = new long[m][l];
long[][] c = new long[n][l];
//Matrix A
for(int i=0;i<n;i++)for(int j=0;j<m;j++){
a[i][j]=scan.nextInt();
}
//Matrix B
for(int i=0;i<m;i++)for(int j=0;j<l;j++){
b[i][j]=scan.nextInt();
}
//Matrix product c=a*b
for(int i=0;i<n;i++){
for(int j=0;j<l;j++){
for(int k=0;k<m;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
// System.out.println(Arrays.deepToString(c));
StringBuilder sb = new StringBuilder();
for(int i=0;i<n;i++){
for(int j=0;j<l;j++){
sb.append(c[i][j]);
if(j != l-1) sb.append(" ");
}
if(i != n-1) sb.append("\n");
}
System.out.println(sb);
scan.close();
}
}
Recommended Posts