[Java] Data type / matrix product (AOJ ⑧ Matrix product)

Data type (data type)

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

Matrix product (ITP1-7)

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

[Java] Data type / matrix product (AOJ ⑧ Matrix product)
[Java] Data type ①-Basic type
Java learning memo (data type)
Java date data type conversion (Date, Calendar, String)
Use PostgreSQL data type (jsonb) from Java
[Personal memo] Java data type is annoying
[Java] Data type / string class cheat sheet
[JAVA] Stream type
[Java] Enumeration type
Java Optional type
Java double type
[Processing x Java] Data type and object-oriented programming
Organized memo in the head (Java --Data type)
Java variable declaration, initialization, data type (cast and promotion)
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
About Java basic data types and reference type memory
[Java] Main data types
[Java] About enum type
Java basic data types
[Java] Date type conversion
[Java] Character acquisition / boolean type usage (AOJ⑤ Finding missing cards)
[Java] List type / Array type conversion
[Java] Extract substrings (AOJ13 --shuffle)
Basic data type and reference type
Try functional type in Java! ①
[Java] Precautions for type conversion
Importing Excel data in Java 2
[Java] Type conversion speed comparison
Java study # 7 (branch syntax type)
Import Excel data in Java
Java addition excel data validation
Java for beginners, data hiding
Importing Excel data in Java 3
Java Primer Series (Type Conversion)
List data structure [Java / Scala]
Java Learning 1 (learning various data types)
[Introduction to Java] Variables and types (variable declaration, initialization, data type)