CS/BaekJoon
[BaekJoon] 백준 2738번 행렬 덧셈 - Java
Bell91
2024. 4. 2. 15:58
반응형
🎈문제
https://www.acmicpc.net/problem/2738
💬설명
- 행렬은 배열을 2개이상 묶어서 사용한다
- 2x2 행렬 Integer[][]
- 3x3 행렬 Integer[][][]
- 동일하게 데이터 안에 값을 넣어 크기를 지정한다
⌨️ CODE
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N, M;
String[] input1 = br.readLine().split(" ");
N = Integer.parseInt(input1[0]);
M = Integer.parseInt(input1[1]);
Integer[][] num1 = new Integer[N][M];
Integer[][] num2 = new Integer[N][M];
Integer[][] num3 = new Integer[N][M];
for(int i = 0 ; i < N ; i ++) {
String[] input2 = br.readLine().split(" ");
for(int j = 0 ; j < M ; j++) {
num1[i][j] = Integer.parseInt(input2[j]);
}
}
for(int i = 0 ; i < N ; i ++) {
String[] input2 = br.readLine().split(" ");
for(int j = 0 ; j < M ; j++) {
num2[i][j] = Integer.parseInt(input2[j]);
}
}
for(int i = 0 ; i < N ; i ++) {
for(int j = 0 ; j < M ; j++) {
num3[i][j] = num1[i][j] + num2[i][j];
}
}
for(int i = 0 ; i < N ; i ++) {
for(int j = 0 ; j < M ; j++) {
System.out.print(num3[i][j] + " ");
}
System.out.println();
}
}
}
반응형