https://www.acmicpc.net/problem/2738
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[][] A = new int[N][M];
int[][] B = new int[N][M];
for(int i =0 ; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) {
A[i][j] = Integer.parseInt(st.nextToken());
}
}
for(int i =0 ; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) {
B[i][j] = Integer.parseInt(st.nextToken());
}
}
// 더하기
StringBuilder sb = new StringBuilder();
for(int i =0 ; i < N; i++) {
for(int j = 0; j < M; j++) {
sb.append((A[i][j] + B[i][j]) + " ");
}
sb.append("\n");
}
System.out.println(sb.toString());
}
}
1) 입출력 기능이 들어갔으므로 throws IOException을 붙여주면 좋다.
2) Scanner 대신 BufferReader를 사용하면 더 빠르다. 또한 BufferReader는 StringTokenizer와 자주 사용된다.
3) 두번 입력받아야 하기 때문에 for문을 두번 써주면 된다 :)
4) 더하기 -> A[0][0] + B[0][0]을 하기 때문에 이중 반복문을 사용한다.
5) + " ", 즉 공백을 더하는 이유는 우리가 출력할 값에 숫자들 사이에 공백을 넣기 때문이다.
ex)
2 3 5
1 5 9