1.程序编写
#include<stdio.h>
void show(int *p3, int lenc)
{
for (int i = 0; i < lenc; i++)
{
printf("%d,",p3[i] );
}
}
void Compare(int *p1, int lena, int *p2, int lenb, int *p3)
{
int i, j, k;
i = j = k=0;
while (i < lena && j < lenb)
{
if (p1[i] < p2[j])
p3[k++] = p1[i++];
else
p3[k++] = p2[j++];
}
while (i < lena)
{
p3[k++] = p1[i++];
}
while (j < lenb)
{
p3[k++] = p2[j++];
}
}
void main()
{
int a[] = { 1,3,5,7,9 ,10,15,21};
int b[] = { 2,4,6,8,10,18 };
int c[100];
int lena = sizeof(a) / sizeof(a[0]);
int lenb = sizeof(b) / sizeof(b[0]);
int lenc = lena + lenb;
Compare(a,lena, b,lenb,c);
show(c, lenc);
}
2.结果演示
归并: 把两个已按升序排列的数组合并成一个升序数组,要求用函数实现
最新推荐文章于 2023-11-02 15:22:10 发布