题目描述:
给你两个升序数组,把他们合并为一个升序数组并输出
代码:
#include <iostream>
using namespace std;
const int MAXN=10000;
int nums1[MAXN];
int nums2[MAXN];
int main()
{int n1,n2;
scanf("%d",&n1);
for(int i=0;i<n1;i++){
scanf("%d",&nums1[i]);
}
scanf("%d",&n2);
for(int j=0;j<n2;j++){
scanf("%d",&nums2[j]);
}
int answer[n1+n2];
int i=0;
int j=0;
int k=0;
while(i<n1&&j<n2){
if(nums1[i]<=nums2[j]){
answer[k++]=nums1[i];
i++;
}
else{
answer[k++]=nums2[j];
j++;
}
}
while(i<n1){
answer[k++]=nums1[i++];
}
while(j<n2){
answer[k++]=nums2[j++];
}
for(int index=0;index<n1+n2;index++){
printf("%d\n",answer[index]);
}
return 0;
}