오름차순으로 정렬이 된 두 배열이 주어지면 두 배열을 오름차순으로 합쳐 출력하는 프로그램을 작성하세요.
첫 번째 줄에 첫 번째 배열의 크기 N(1<=N<=100)이 주어집니다.
두 번째 줄에 N개의 배열 원소가 오름차순으로 주어집니다.
세 번째 줄에 두 번째 배열의 크기 M(1<=M<=100)이 주어집니다.
네 번째 줄에 M개의 배열 원소가 오름차순으로 주어집니다.
각 배열의 원소는 int형 변수의 크기를 넘지 않습니다.
오름차순으로 정렬된 배열을 출력합니다.
내 풀이
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n, m;
vector<int> v, v2;
cin >> n;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
v.push_back(x);
}
cin >> m;
for (int i = 0; i < m; i++)
{
int y;
cin >> y;
v2.push_back(y);
}
vector<int>::iterator it = v.insert(v.end(), v2.begin(), v2.end());
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[101], b[101], c[300];
int main()
{
int n, m, p1 = 1, p2 = 1, p3 = 1;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
cin >> m;
for (int i = 1; i <= m; i++)
cin >> b[i];
while (p1 <= n && p2 <= m)
{
if (a[p1] < b[p2])
c[p3++] = a[p1++];
else
c[p3++] = b[p2++];
}
while (p1 <= n) c[p3++] = a[p1++];
while (p2 <= m) c[p3++] = b[p2++];
for (int i = 1; i < p3; i++)
cout << c[i] << " ";
}