2017. 12. 6. 14:39
프로그래머스 Level 1.행렬의 덧셈
문제 ----------------------------------------------------------
행렬의 덧셈은 행과 열의 크기가 같은 두 행렬의 같은 행, 같은 열의 값을 서로 더한 결과가 됩니다. 2개의 행렬을 입력받는 sumMatrix 함수를 완성하여 행렬 덧셈의 결과를 반환해 주세요.
예를 들어 2x2 행렬인 A = ((1, 2), (2, 3)), B = ((3, 4), (5, 6)) 가 주어지면, 같은 2x2 행렬인 ((4, 6), (7, 9))를 반환하면 됩니다.(어떠한 행렬에도 대응하는 함수를 완성해주세요.)
----------------------------------------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<vector> | |
using namespace std; | |
vector<vector<int> > sumMatrix(vector<vector<int> >A, vector<vector<int> >B) | |
{ | |
vector<vector<int>> answer; | |
for (int i = 0; i < A.size(); i++) | |
{ | |
std::vector<int> col; | |
for (int j = 0; j < A[i].size(); j++) | |
{ | |
int sum = A[i][j] + B[i][j]; | |
col.emplace_back(sum); | |
} | |
answer.emplace_back(col); | |
} | |
return answer; | |
} | |
int main() | |
{ | |
vector<vector<int> > a{{1,2},{2,3}}, b{{3,4},{5,6}}; | |
vector<vector<int> > answer = sumMatrix(a,b); | |
for(int i=0;i<answer.size();i++) | |
{ | |
for(int j=0;j<answer[0].size();j++) | |
{ | |
cout<<answer[i][j]<<" "; | |
} | |
cout<<"\n"; | |
} | |
} |
'Programming > C++' 카테고리의 다른 글
알고리즘 문제 예제 2 (0) | 2017.12.06 |
---|---|
알고리즘 문제 예제 1 (0) | 2017.12.06 |
프로그래머스 Level 1.피보나치 수 (0) | 2017.12.05 |
[C++]최대공약수, 최소공배수 구하기 (0) | 2017.10.09 |
[C++]바이트에서 1인 비트의 개수 세기 (0) | 2017.09.15 |