已知两个升序数组a,b及空数组c,编写程序将两个数组按大小顺序完成归并,并存入c中。
int a[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int b[] = {2, 4, 6, 8, 10, 12, 14, 16, 18,20,22,24};
int c[30]={};
#include <stdio.h>
#define N sizeof(a) / sizeof(int)
#define M sizeof(b) / sizeof(int)
int a[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int b[] = {2, 4, 6, 8, 10, 12, 14, 16, 18,20,22,24};
int c[30]={};
int i=0,j=0,k=0,*p = a,*q = b,*m = c;
int main(int argc, char const *argv[])
{
//在数组a和数组b都有数据时比较两个数组
while (p<a+N && q<b+M)//以地址为判断标准
{
if (*p<*q)
{
*m=*p;
m++;
p++;
}else{
*m=*q;
m++;
q++;
}
}
//数组b还有元素,打印输出剩下的元素
while(q<b+M)
{
*m=*q;
m++;
q++;
}
for(k=0;k<30;k++)
{
printf("%d ",c[k]) ;
}
putchar(10);
return 0;
}
运行结果: