백준 단계별
[Java] 백준 2738: 항렬 덧셈
pullwall
2023. 1. 25. 15:34
728x90
import java.util.*;
public class no2738 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int[][] arr1 = new int[N][M];
int[][] arr2 = new int[N][M];
for(int i =0 ; i<N ; i++) {
for(int j=0 ; j<M ; j++) {
arr1[i][j]=sc.nextInt();
}
}
for(int i =0 ; i<N ; i++) {
for(int j=0 ; j<M ; j++) {
arr2[i][j]=sc.nextInt();
}
}
for(int i=0 ; i<N ; i++) {
for(int j=0 ; j<M ; j++) {
System.out.print(arr1[i][j]+arr2[i][j]+" ");
if(j==M-1) {
System.out.println();
}
}
}
}
}
2개의 행렬을 저장할 2차원 배열을 선언하고
nextInt를 통해 2차원 배열에 행렬값들을 입력한다.
행렬의 덧셈은 위치별로 각자 더해주면 되기 때문에 이를 진행하고
주의할 점은 j==M-1 즉, 3x3행렬이라면 j가 행렬의 행 끝까지 갔을 때
한줄 띄어줘야 한다는 것이다.
728x90