백준 2738 : 행렬덧셈 B5
2738번: 행렬 덧셈
- A, B 각각 2차원 배열을 선언해서 값을 넣는다
- 두 2차원 배열의 행렬 크기는 동일함
import java.util.Scanner;
public class matrix_plus {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int N = sc.nextInt(), M = sc.nextInt();
int [][] A = new int [N][M];
int [][] B = new int [N][M];
for (int i = 0; i<N;i++){
for(int j = 0; j<M; j++){
A[i][j] = sc.nextInt();
}
}
for(int k = 0; k<N; k++ ){
for(int q=0; q<M;q++){
B[k][q] = sc.nextInt();
}
}
for(int i=0; i<N; i++){
for (int j =0; j < M;j++){
int result = A[i][j] + B[i][j];
System.out.print(result + " ");
}
System.out.print("\n");
}
}
}
- 하나의 행을 열 (M) 값 만큼 반복하면서 원소 값을 넣어주고 다시 이중 반복문으로 동일한 행열 값을 갖는 다른 2차원 배열의 값을 result에 저장 result 출력하고 하나의 행이 종료되면 줄 바꿈을 해줌