알고리즘
백준 BOJ 11728 배열 합치기 c++
merge sort
이용
#include <bits/stdc++.h>
using namespace std;
int n, m;
void mSort(int *arr) {
int temparr1[n];
int temparr2[m];
for (int i = 0; i < n; i++)
cin >> temparr1[i];
for (int i = 0; i < m; i++)
cin >> temparr2[i];
int i = 0, j = 0;
int index = 0;
while (i < n && j < m) {
if (temparr1[i] < temparr2[j])
arr[index++] = temparr1[i++];
else
arr[index++] = temparr2[j++];
}
while (i < n)
arr[index++] = temparr1[i++];
while (j < m)
arr[index++] = temparr2[j++];
for (int k = 0; k < n + m; k++) {
cout << arr[k] << ' ';
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
int arr[n + m];
mSort(arr);
}
STL sort
이용
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
int arr1[n];
int arr2[m];
int arr[n + m];
int i;
for (i = 0; i < n; i++)
cin >> arr[i];
for (int j = 0; j < m; j++)
cin >> arr[i++];
sort(arr, arr + n + m);
for (int k = 0; k < n + m; k++) {
cout << arr[k] << ' ';
}
}
'Algorithm' 카테고리의 다른 글
BOJ 9663 c++ (0) | 2020.07.14 |
---|---|
BOJ 1182 c++ (0) | 2020.07.14 |
BOJ 15694 c++ (0) | 2020.07.13 |
BOJ 1074 c++ (0) | 2020.07.13 |
BOJ 11729 하노이 탑 이동 순서 c++ (0) | 2020.07.13 |
최근댓글