1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Java】データ型・行列の積(AOJ⑧行列の積)

Last updated at Posted at 2020-11-15

データ型(データの種類)

  • データの種類と大きさにあったデータ型を使う
    • byte, char:バイトデータを格納する
    • short:小さな整数を表す
    • long:intで対応できない数値範囲を扱う場合のみ
  • 参考:
    【Java】データ型①-基本型
public class Main {
    public static void main(String[] args){
        //int型の最小値と最大値を確認する
        System.out.println("int   最小/最大値= " + Integer.MIN_VALUE+" ~ "+Integer.MAX_VALUE);
        System.out.println("byte  最小/最大値= " + Byte.MIN_VALUE+" ~ "+Byte.MAX_VALUE);
        System.out.println("short 最小/最大値= " + Short.MIN_VALUE+" ~ "+Short.MAX_VALUE);
        System.out.println("long  最小/最大値= " + Long.MIN_VALUE+" ~ "+Long.MAX_VALUE);
    }
}
int   最小/最大値= -2147483648 ~ 2147483647
byte  最小/最大値= -128 ~ 127
short 最小/最大値= -32768 ~ 32767
long  最小/最大値= -9223372036854775808 ~ 9223372036854775807

行列の積(ITP1-7)

n×m の行列 Aと m×l の行列 B を入力し、それらの積である n×l の行列 C を出力するプログラムを作成してください。
ここで、A、B、C の各要素をそれぞれ aij、bij、cij とします。
Input
1行目に n、m、l が空白区切りで与えられます。
続く行に n×m の行列 A と m×l の行列 B が与えられます。
Output
n×l の行列 Cの要素 cijを出力してください。各行の隣り合う要素を1つの空白で区切ってください。
Constraints

  • 1≤n,m,l≤100
  • 0≤aij,bij≤10000

//内積計算
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なので
        //int型の最大範囲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];

        //行列A
        for(int i=0;i<n;i++)for(int j=0;j<m;j++){
            a[i][j]=scan.nextInt();
        }

        //行列B
        for(int i=0;i<m;i++)for(int j=0;j<l;j++){
            b[i][j]=scan.nextInt();
        }
        
        //行列の積 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();
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?