알고리즘 코드카타
1. 행렬의 덧셈
첨에 문제와 예시를 봤을 때 행이 2로 고정인 줄 알고 for 문을 2로 돌렸는데 틀렸다..
그래서 사이즈 값으로 for문 다시 돌려서 완료.
arr1과 arr2의 값이 같으므로 arr2의 값을 arr1에 더해서 리턴.
#include <string>
#include <vector>
using namespace std;
vector<vector<int>> solution(vector<vector<int>> arr1, vector<vector<int>> arr2) {
for(int i=0;i<arr1.size();i++){
for(int j=0;j<arr1[i].size();j++){
arr1[i][j]+=arr2[i][j];
}
}
return arr1;
}
근데 이제 이 방법은 간단하지만 원본 값을 수정하는 방식...
좋지 않은거 같아 answer에 값을 더해 저장했다.
#include <string>
#include <vector>
using namespace std;
vector<vector<int>> solution(vector<vector<int>> arr1, vector<vector<int>> arr2) {
vector<vector<int>> answer;
for(int i=0;i<arr1.size();i++){
vector<int> temp;
for(int j=0;j<arr1[i].size();j++){
temp.push_back(arr1[i][j]+arr2[i][j]);
}
answer.push_back(temp);
}
return answer;
}
2. 직사각형 별찍기
#include <iostream>
using namespace std;
int main(void) {
int a;
int b;
cin >> a >> b;
for(int i=0;i<b;i++){
for(int j=0;j<a;j++){
cout<<"*";
}
cout<<endl;
}
return 0;
}
3. 최대공약수와 최소공배수
#include <string>
#include <vector>
using namespace std;
vector<int> solution(int n, int m) {
vector<int> answer;
int big=0;
int small=0;
if(n<m){
small=n;
big=m;
}
else{
small=m;
big=n;
}
while(1){
if(big%small==0)
{
answer.push_back(small);
break;
}
else{
int temp=small;
small=big%small;
big=temp;
}
}
answer.push_back(n*m/answer[0]);
return answer;
}
유클리드 호제법
2개의 자연수(또는 정식) a, b에 대해서 a를 b로 나눈 나머지를 r이라 하면(단, a>b), a와 b의 최대공약수는 b와 r의 최대공약수와 같다. 이 성질에 따라, b를 r로 나눈 나머지 r'를 구하고, 다시 r을 r'로 나눈 나머지를 구하는 과정을 반복하여 나머지가 0이 되었을 때 나누는 수가 a와 b의 최대공약수이다.
4. 참고
- 유클리드 호제법

'내배캠Unreal_TIL > C++' 카테고리의 다른 글
| [TIL] 2026-01-19 | C++ Set 사용 (0) | 2026.01.19 |
|---|---|
| [TIL] 2026-01-15 | C++ 캐스트(cast) 연산자 (0) | 2026.01.15 |
| [TIL] 2025-12-29 | C++에서 중요한 건 메모리, 수명, 비용 (0) | 2025.12.29 |
| [TIL] 2025-12-23 | C++ 하면서 사소한 것들 (0) | 2025.12.23 |
| [TIL] 2025-12-22 | C++ 디자인패턴 - 싱글톤, 데코레이터, 옵저버 정리 (0) | 2025.12.22 |