视频讲解👇:
p18 第10题 c语言代码实现王道数据结构课后代码题_哔哩哔哩_bilibili
本题代码如下
int search(int a[], int b[], int c[])
{
int i = 0;
int j = 0;
int k = 0;
while (i < 5 && j < 5)
{
if (a[i] <= b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while (i < 5)
c[k++] = a[i++];
while (j < 5)
c[k++] = b[j++];
return c[(k - 1) / 2];
}
完整测试代码
#include<stdio.h>
int a[5] = { 11,13,15,17,19 };
int b[5] = { 2,4,6,8,20 };
int n = 5;
int c[10];
int search(int a[], int b[], int c[])
{
int i = 0;
int j = 0;
int k = 0;
while (i < 5 && j < 5)
{
if (a[i] <= b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while (i < 5)
c[k++] = a[i++];
while (j < 5)
c[k++] = b[j++];
return c[(k - 1) / 2];
}
int main()
{
int ret = search(a, b, c);
for (int i = 0; i < 10; i++)
printf("%d ", c[i]);
printf("中位数为:%d", ret);
return 0;
}