Last active
December 16, 2016 13:12
-
-
Save mokomokohitsuzi/a8b881dec4233b509e334d94b3108574 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class En06_16 { | |
public static void main(String[] args) { | |
Scanner stdIn = new Scanner(System.in); | |
final int max = 4; | |
final int d = 3; | |
// 4行3列の行列を作成 | |
int[][] a = new int[max][d]; | |
// 3行4列の行列を作成 | |
int[][] b = new int[d][max]; | |
// 解答用の4列4列の配列を作成 | |
int[][] c = new int[max][max]; | |
System.out.println("配列a"); | |
for (int i = 0; i < max; i++) { | |
for (int j = 0; j < d; j++) { | |
System.out.printf("a[%d][%d]:", i, j); | |
a[i][j] = stdIn.nextInt(); | |
} | |
} | |
System.out.println("配列b"); | |
for (int i = 0; i < d; i++) { | |
for (int j = 0; j < max; j++) { | |
System.out.printf("b[%d][%d]:", i, j); | |
b[i][j] = stdIn.nextInt(); | |
} | |
} | |
System.out.println(); | |
// 配列cに配列a*配列bの積を代入する。 | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < b[0].length; j++) { | |
for (int k = 0; k < a[0].length; k++) { | |
c[i][j] += a[i][k] * b[k][j]; | |
} | |
} | |
} | |
System.out.println(); | |
// 配列aの表示 | |
System.out.println("配列a"); | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < a[0].length; j++) { | |
System.out.printf(" %d", a[i][j]); | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
System.out.println("配列b"); | |
// 配列bの表示 | |
for (int i = 0; i < b.length; i++) { | |
for (int j = 0; j < b[0].length; j++) { | |
System.out.printf(" %d", b[i][j]); | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
System.out.println("配列c"); | |
// 配列cの表示 | |
for (int i = 0; i < c.length; i++) { | |
for (int j = 0; j < c[0].length; j++) { | |
System.out.printf(" %3d", c[i][j]); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment