ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [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โœจ]

    ๋ฐ˜์‘ํ˜•
Designed by Tistory.