用c语言编写:将两个升序的数组归并成一个新的升序数组

如题所述

//之前写过的,你可以参考下

#include <stdio.h>
int main()
{

int str1[5]={3,6,7,45,55};//两个升序数组
int str2[5]={8,10,11,22,25};
int out[10];//输出数组
int i=0,j=0,k=0;
while (i<5&&j<5)
{//循环将较小元素放入C
if (str1[i]<str2[j])
{
out[k]=str1[i];
i++;
k++;
    }
else
{
 out[k]=str2[j];
 j++;
 k++;
    }
}//while
if(i==5)
{//第1个数组元素已经全部放到C中,将第2个数组剩余元素全放到C中
while (j<5)
{
out[k]=str2[j];
k++;
j++;
}
}
if(j==5)
{//第2个数组元素已经全部放到C中,将第1个数组剩余元素全放到C中

while (i<5)
{
out[k]=str1[i];
k++;
i++;
    }
}
for(int i=0;i<10;i++)
{
    printf("%d ",out[i]);
}
}

温馨提示:答案为网友推荐,仅供参考
相似回答