forked from GD2032/AtividadesDePad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OPEI Multiplicar matriz.txt
51 lines (48 loc) · 1.52 KB
/
OPEI Multiplicar matriz.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x1 = scan.nextInt();
int y1 = scan.nextInt();
int[][] matriz1 = new int[x1][y1];
for(int i = 0; i < x1; i++){
for(int j = 0; j < y1; j++){
matriz1[i][j] = scan.nextInt();
}
}
int x2 = scan.nextInt();
int y2 = scan.nextInt();
int[][] matriz2 = new int[x2][y2];
for(int i = 0; i < x2; i++){
for(int j = 0; j < y2; j++){
matriz2[i][j] = scan.nextInt();
}
}
if(y1 != x2){
System.out.println(-1);
}
else{
int[][] matrizF = new int [x1][y2];
int valor = 0;
for(int i = 0; i < x1; i++){
for(int j = 0; j < y2; j++){
for(int k = 0; k < y1; k++){
valor += matriz1[i][k] * matriz2 [k][j];
}
matrizF[i][j] = valor;
valor = 0;
}
}
for(int i = 0; i < x1; i++){
for(int j = 0; j < y2; j++){
if(j == y2 - 1){
System.out.println(matrizF[i][j]);
}
else{
System.out.print(matrizF[i][j] + " ");
}
}
}
}
}
}