본문 바로가기

Coding Test/백준

[백준/자바] 2738 - 행렬 덧셈

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));
        String[] nm = br.readLine().split(" ");
        int n = Integer.parseInt(nm[0]);
        int m = Integer.parseInt(nm[1]);
        int[][] matrix = new int[n][m];

        for(String s : nm) {
            for (int i = 0; i < n; i++) {
                String[] bits = br.readLine().split(" ");
                for (int j = 0; j < m; j++) {
                    matrix[i][j] += Integer.parseInt(bits[j]);
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                sb.append(matrix[i][j]).append(" ");
            }
            sb.append("\n");
        }

        System.out.println(sb);

        br.close();
    }
}

'Coding Test > 백준' 카테고리의 다른 글

[백준/자바] 2566 - 최댓값  (0) 2023.05.11
[백준/자바] 9012 - 괄호  (0) 2023.05.11
[백준/자바] 1157 - 단어 공부  (0) 2023.05.11
[백준/자바] 2444 - 별 찍기 - 7  (0) 2023.05.11
[백준/자바] 10828 - 스택  (0) 2023.05.11