알고리즘 풀이

Level 1 행렬의 덧셈

bedst 2017. 3. 30. 17:37

/*

 * http://tryhelloworld.co.kr/challenge_codes/7

 */

class SumMatrix {

int[][] sumMatrix(int[][] A, int[][] B) {

int[][] answer = new int[2][2];

int sum=0;

int n=0;

int num1[] = new int[4];

int num2[] = new int[4];


for(int i=0; i<2; i++){

for(int j=0; j<2; j++){

num1[sum]=A[i][j];

num2[sum]=B[i][j];

sum++;

}

}

for(int i=0; i<2; i++){

for(int j=0; j<2; j++){

answer[i][j]=num1[n]+num2[n];

n++;

System.out.println(answer[i][j]);

}

}

return answer;

}


// 아래는 테스트로 출력해 보기 위한 코드입니다.

public static void main(String[] args) {

SumMatrix c = new SumMatrix();

int[][] A = { { 1, 2 }, { 2, 3 } };

int[][] B = { { 3, 4 }, { 5, 6 } };

int[][] answer = c.sumMatrix(A, B);

if (answer[0][0] == 4 && answer[0][1] == 6 && 

answer[1][0] == 7 && answer[1][1] == 9) {

System.out.println("맞았습니다. 제출을 눌러 보세요");

} else {

System.out.println("틀렸습니다. 수정하는게 좋겠어요");

}

}

}