代码演示:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N],b[N],c[2*N];
void merge(int a[],int n,int b[],int m) { //合并函数
int i = 0,j = 0,tot = 0;
while (i < n && j < m) {
if (a[i] < b[j]) {
c[tot++] = a[i];
i++;
} else {
c[tot++] = b[j];
j++;
}
}
while (i < n) c[tot++] = a[i++]; //重新赋值
while (j < m) c[tot++] = b[j++];
for (int i = 0;i < tot;i++) printf("%d ",c[i]); //在void函数里输出
}
int main() {
int n,m;
scanf("%d%d",&n,&m);
for (int i = 0;i < n;i++) scanf("%d",&a[i]);
for (int j = 0;j < m;j++) scanf("%d",&b[j]);
merge(a,n,b,m);
return 0;
}