-
[JAVA] ํ๋ ฌ ์ฐ์ฐ_๋ง์ ๋บ์ ๊ณฑ์ / ๋๋คํจ์ ์ฌ์ฉํ๊ธฐDEV ๐/JAVA 2020. 3. 10. 18:18๋ฐ์ํ
1 ํ๊ณผ ์ด ํฌ๊ธฐ ์ง์ ๊ณผ ํ๋ ฌ ์์ฑ
int row, col; //rowํ, col์ด ํฌ๊ธฐ Scanner scan = new Scanner(System.in); //์ค์บ๋ System.out.print("ํ(row) : "); row = Integer.parseInt(scan.nextLine()); System.out.print("์ด(col) : "); col = Integer.parseInt(scan.nextLine()); int[][] matrix = new int[row][col]; //์ ๋ ฅ๋ฐ์ ํฌ๊ธฐ๋ก ํ๋ ฌ1 ์์ฑ
2 ๋๋ค๊ฐ ์ง์
1~10 ์ฌ์ด์ ์ซ์๋ฅผ ๋๋ค ์ง์
Math.random() ํจ์
for(int i=0; i<matrix2.length ; i++) { for(int j=0; j<matrix2[i].length ;j++) { matrix2[i][j]=(int)(Math.random()*10+1); } }
3 ๋ง์ , ๋บ์
๋ง์ ๊ณผ ๋บ์ ์ ์ฐ์ฐํ๋ ค๊ณ ํ๋ ๋ ํ๋ ฌ์ ํ๊ณผ ์ด ํฌ๊ธฐ๊ฐ ๋ชจ๋ ๊ฐ์์ผ ํ๋ค
if(row==r && col==c) { //col,row : ํ๋ ฌ1 / r,c : ํ๋ ฌ2 System.out.println("ํ๋ ฌ1 + ํ๋ ฌ2"); printm(sum(matrix, matrix2)); //์ฐ์ฐ ํจ์ } else { System.out.println("๋ง์ ๋ถ๊ฐ"); }
์ฐ์ฐ ํจ์
public static int[][] sum(int[][] arr1, int[][] arr2) { int[][] res = new int[arr1.length][arr1[0].length]; for(int i=0;i<arr1.length;i++) { for(int j=0;j<arr1[i].length;j++) { res[i][j] = arr1[i][j] + arr2[i][j]; //์ฐ์ฐ(๋บ์ ์ '+' -> '-') } } return res; // ๊ฒฐ๊ณผ ๋ฐฐ์ด ๋ฐํ }
4 ๊ณฑ์
๊ณฑ์ ์ ๊ฒฝ์ฐ ์์ ํ๋ ฌ์ ์ด(column)๊ณผ ๋ค ํ๋ ฌ์ ํ(row)์ ํฌ๊ธฐ๊ฐ ๊ฐ์์ผ ํ๋ค
if(col==r) { System.out.println("\nํ๋ ฌ1 * ํ๋ ฌ2"); printm(mul(matrix, matrix2)); } else { System.out.println("\n๊ณฑ์ ๋ถ๊ฐ"); }
์ฐ์ฐ ํจ์
ํ๋ ฌ ๊ณฑ ๊ท์น
public static int[][] mul(int[][] arr1, int[][] arr2) { int[][] res = new int[arr1.length][arr2[0].length]; for(int i=0;i<arr1.length;i++) { for(int j=0;j<arr2[i].length;j++) { for(int a=0;a<arr1[0].length;a++) { res[i][j] += arr1[i][a] * arr2[a][j]; } } } return res; }
5 ์ ์ฒด ์ฝ๋
package MatrixCalc; import java.util.Scanner; public class MatrixCalc { //ํ๋ ฌ ๋ง์ public static int[][] sum(int[][] arr1, int[][] arr2) { int[][] res = new int[arr1.length][arr1[0].length]; for(int i=0;i<arr1.length;i++) { for(int j=0;j<arr1[i].length;j++) { res[i][j] = arr1[i][j] + arr2[i][j]; } } return res; } //ํ๋ ฌ ๋บ์ public static int[][] sub(int[][] arr1, int[][] arr2) { int[][] res = new int[arr1.length][arr1[0].length]; for(int i=0;i<arr1.length;i++) { for(int j=0;j<arr1[i].length;j++) { res[i][j] = arr1[i][j] - arr2[i][j]; } } return res; } //ํ๋ ฌ ๊ณฑ์ public static int[][] mul(int[][] arr1, int[][] arr2) { int[][] res = new int[arr1.length][arr2[0].length]; for(int i=0;i<arr1.length;i++) { for(int j=0;j<arr2[i].length;j++) { for(int a=0;a<arr1[0].length;a++) { res[i][j] += arr1[i][a] * arr2[a][j]; } } } return res; } //ํ๋ ฌ ์ถ๋ ฅ public static void printm(int[][] arr) { for(int i=0;i<arr.length;i++) { for(int j=0;j<arr[i].length;j++) { System.out.print(arr[i][j] + "\t"); } System.out.println(); } } public static void main(String[] args) { int row, col, r, c; //rowํ, col์ด ํฌ๊ธฐ Scanner scan = new Scanner(System.in); System.out.print("ํ๋ ฌ1 ํ(row) : "); row = Integer.parseInt(scan.nextLine()); System.out.print("ํ๋ ฌ1 ์ด(col) : "); col = Integer.parseInt(scan.nextLine()); int[][] matrix = new int[row][col]; //์ ๋ ฅ๋ฐ์ ํฌ๊ธฐ๋ก ํ๋ ฌ1 ์์ฑ //ํ๋ ฌ ์ซ์๊ฐ 1-10์ค ๋๋ค ์ง์ for(int i=0; i<matrix.length ; i++) { for(int j=0; j<matrix[i].length ;j++) { matrix[i][j]=(int)(Math.random()*10+1); } } System.out.println(); System.out.print("ํ๋ ฌ2 ํ(row) : "); r = Integer.parseInt(scan.nextLine()); System.out.print("ํ๋ ฌ2 ์ด(col) : "); c = Integer.parseInt(scan.nextLine()); int[][] matrix2 = new int[r][c]; //ํ๋ ฌ2 ์ซ์๊ฐ 1-10์ค ๋๋ค ์ง์ for(int i=0; i<matrix2.length ; i++) { for(int j=0; j<matrix2[i].length ;j++) { matrix2[i][j]=(int)(Math.random()*10+1); } } //ํ๋ ฌ ์ถ๋ ฅ System.out.println("\nํ๋ ฌ1"); printm(matrix); System.out.println(); System.out.println("ํ๋ ฌ2"); printm(matrix2); System.out.println(); //๋ง์ , ๋บ์ if(row==r && col==c) { System.out.println("ํ๋ ฌ1 + ํ๋ ฌ2"); printm(sum(matrix, matrix2)); System.out.println("\nํ๋ ฌ1 - ํ๋ ฌ2"); printm(sub(matrix, matrix2)); } else { System.out.println("๋ง์ ๋ถ๊ฐ"); System.out.println("๋บ์ ๋ถ๊ฐ"); } //๊ณฑ์ if(col==r) { System.out.println("\nํ๋ ฌ1 * ํ๋ ฌ2"); printm(mul(matrix, matrix2)); } else { System.out.println("\n๊ณฑ์ ๋ถ๊ฐ"); } } }
์ถ์ฒ: https://alliwannado-start.tistory.com/9 [NOTEโจ]๋ฐ์ํ'DEV ๐ > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Javascript/HTML] ํ์ผ ๋ค์ด๋ก๋ ์ ํ์ผ์ด๋ฆ ๋ฐ๊พธ๊ธฐ (0) 2023.03.17 [JAVA] ์์ ํ์ผ(.xlsx) ์ฐ๊ธฐ / ์คํ์ผ ์ ์ฉ (2) 2020.03.17 [JAVA] ์์ ํ์ผ(.xlsx) ์ฝ์ด์ค๊ธฐ_POIํ์ฉ (0) 2020.03.10 [JAVA] ์๋ฐ ์์ ํ์ผ ์์ _Apache POI ๋ผ์ด๋ธ๋ฌ๋ฆฌ (0) 2020.03.10