思想:合并之后,若表长为偶数,则取中间两个的平均数若为奇数,则取中间的数
#include <stdio.h>
#define maxsize 10
typedef struct
{
int data[maxsize];
int length;
}sqlist;
float func(sqlist A, sqlist B, sqlist& C)
{
int i = 0, j = 0, k = 0;
while (i < A.length && j < B.length)
{
if (A.data[i] < B.data[j])
{
C.data[k++] = A.data[i++];//A的值小,A中元素并入C
}
else
C.data[k++] = B.data[j++];//B的值小,B中元素并入C
}
while (j < B.length)
C.data[k++] = B.data[j++];//如果循环结束,表B还有元素没并入C,则一股脑全部并入
while (i < A.length)
C.data[k++] = A.data[i++];//如果循环结束,表A还有元素没并入C,则一股脑全部并入
C.length = A.length + B.length;
if (C.length % 2 == 0)
return (C.data[(C.length / 2)] + C.data[(C.length / 2) - 1]) / 2.0;
else
return C.data[C.length / 2];
}
int main()
{
sqlist A = { {1,3,5},3 };
sqlist B = { {2,4,6},3 };
sqlist C = { {},};
float c = func(A, B, C);
printf("%f", c);
return 0;
}