fork download
  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. int na = scanner.nextInt();
  6. int ma = scanner.nextInt();
  7. int[][] a = new int[na][ma];
  8. for (int i = 0; i < na; i++) {
  9. for (int j = 0; j < ma; j++) {
  10. a[i][j] = scanner.nextInt();
  11. }
  12. }
  13. int nb = scanner.nextInt();
  14. int mb = scanner.nextInt();
  15. if (ma != nb) {
  16. System.out.print(-1);
  17. return;
  18. }
  19. int[][] b = new int[nb][mb];
  20. for (int i = 0; i < nb; i++) {
  21. for (int j = 0; j < mb; j++) {
  22. b[i][j] = scanner.nextInt();
  23. }
  24. }
  25. int[][] c = new int[na][mb];
  26. for (int i = 0; i < na; i++) {
  27. for (int j = 0; j < mb; j++) {
  28. for (int r = 0; r < ma; r++) {
  29. c[i][j] += a[i][r] * b[r][j];
  30. }
  31. }
  32. }
  33. System.out.print(na + " " + mb + "\n");
  34. for (int i = 0; i < na; i++) {
  35. for (int j = 0; j < mb; j++) {
  36. System.out.print(c[i][j]);
  37. if (j + 1 != mb) System.out.print(" ");
  38. }
  39. System.out.print("\n");
  40. }
  41. }
  42. }
Success #stdin #stdout 0.07s 2184192KB
stdin
4 4
4 8 -18 16
3 7 14 -42
2 1 1 7
4 9 5 -2
4 4
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
stdout
4 4
4 8 -18 16
3 7 14 -42
2 1 1 7
4 9 5 -2