[백준] 2738

당당·2023년 4월 21일
0

백준

목록 보기
36/179

https://www.acmicpc.net/problem/2738

📔문제

N*M크기의 두 행렬 A와 B가 주어졌을 때, 두 행렬을 더하는 프로그램을 작성하시오.


📝입력

첫째 줄에 행렬의 크기 NM이 주어진다. 둘째 줄부터 N개의 줄에 행렬 A의 원소 M개가 차례대로 주어진다. 이어서 N개의 줄에 행렬 B의 원소 M개가 차례대로 주어진다. NM은 100보다 작거나 같고, 행렬의 원소는 절댓값이 100보다 작거나 같은 정수이다.


📺출력

첫째 줄부터 N개의 줄에 행렬 A와 B를 더한 행렬을 출력한다. 행렬의 각 원소는 공백으로 구분한다.


📝예제 입력 1

3 3
1 1 1
2 2 2
0 1 0
3 3 3
4 4 4
5 5 100

📺예제 출력 1

4 4 4
6 6 6
5 6 100

🔍출처

-문제의 오타를 찾은 사람: purpose


🧮알고리즘 분류

  • 수학
  • 구현

📃소스 코드

import java.util.Scanner;

public class Code2738 {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		
		int first=0;
		int second=0;
		first=scanner.nextInt();
		second=scanner.nextInt();
		
		int[][] farray=new int[first][second];
		int[][] sarray=new int[first][second];
		int[][] tarray=new int[first][second];
		
		for(int i=0;i<first;i++) {
			for(int j=0;j<second;j++) {
				farray[i][j]=scanner.nextInt();
			}
		}
		for(int i=0;i<first;i++) {
			for(int j=0;j<second;j++) {
				sarray[i][j]=scanner.nextInt();
			}
		}
		
		for(int i=0;i<first;i++) {
			for(int j=0;j<second;j++) {
				tarray[i][j]=farray[i][j]+sarray[i][j];
			}
		}
		
		for(int i=0;i<first;i++) {
			for(int j=0;j<second;j++) {
				System.out.print(tarray[i][j]+" ");
			}
			System.out.println("");
		}

	}

}

📰출력 결과


📂고찰

바로 덧셈하면서 출력했더니 값이 이상하게 나와서
다 저장해놓고 다시 돌렸다..

profile
MySQL DBA 신입 지원

0개의 댓글