Algorithm
백준 2738 행렬 덧셈
dalooong
2023. 7. 16. 23:34
2738번: 행렬 덧셈
첫째 줄에 행렬의 크기 N 과 M이 주어진다. 둘째 줄부터 N개의 줄에 행렬 A의 원소 M개가 차례대로 주어진다. 이어서 N개의 줄에 행렬 B의 원소 M개가 차례대로 주어진다. N과 M은 100보다 작거나 같
www.acmicpc.net

문제코드
import java.util.Scanner;
public class boj2738 {
public static void main(String[] args) {
// 행렬의 크기 N, M이 주어진다.
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
// 둘째줄부터 N개의 줄에 행렬 A의 원소 M개가 차례대로 주어진다.
int[][] arr1 = new int[n][m];
int[][] arr2 = new int[n][m];
// n과 m은 100보다 작거나 같고, 행렬의 원소는 절대값이 100보다 작거나 같은 정수이다.
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();
}
}
}
}
출력 결과
입력
3 3
1 1 1
2 2 2
0 1 0
3 3 3
4 4 4
5 5 100
출력
4 4 4
6 6 6
5 6 100