题解
2023-08-21 10:41:31
发布于:广东
1阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
int a[maxn], b[maxn], c[maxn<<1];
int main() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int j = 1; j <= m; j++) {
cin >> b[j];
}
int i = 1, j = 1;
int cnt = 0;
while(i <= n && j <= m) {
if(a[i] <= b[j]) {
c[cnt] = a[i];
cnt++;
i++;
}else {
c[cnt] = b[j];
cnt++;
j++;
}
}
while(i <= n) c[cnt++] = a[i++];
while(j <= m) c[cnt++] = b[j++];
for(int i = 0; i < cnt; i++) {
cout << c[i] << " ";
}
return 0;
}
经典的归并
这里空空如也
有帮助,赞一个