题解
2024-05-19 15:28:09
发布于:广东
8阅读
0回复
0点赞
#include <iostream>
#include <vector>
int main() {
int n, m;
std::cin >> n >> m;
std::vector<int> a(n), b(m);
for (int &num : a) std::cin >> num;
for (int &num : b) std::cin >> num;
std::vector<int> c;
int i = 0, j = 0;
while (i < n && j < m) {
if (a[i] <= b[j]) {
c.push_back(a[i++]);
} else {
c.push_back(b[j++]);
}
}
while (i < n) {
c.push_back(a[i++]);
}
while (j < m) {
c.push_back(b[j++]);
}
for (int num : c) {
std::cout << num << " ";
}
return 0;
}
或
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[10005],b[10005],c[10005];
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<m;i++) cin>>b[i];
int i=0,j=0,k=0;
while(i<n and j<m){
if(a[i]>b[j]){
c[k++]=b[j++];
}else{
c[k++]=a[i++];
}
}
while(i<n){
c[k++]=a[i++];
}
while(j<m){
c[k++]=b[j++];
}
for(int ii=0;ii<k;ii++){
cout<<c[ii]<<" ";
}
return 0;
}
这里空空如也
有帮助,赞一个