2020王道P19,综合11:两等长升序序列求处于中间位置的中位数。还是感觉效仿第七题:将两序列合并成一个升序的序列,之后求中位数。具体实现如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define InitSize 100
typedef int ElemType;
typedef struct{
ElemType *data;
int length,Maxsize;
}SeqList;
bool ListInsert(SeqList &L,int i,ElemType e){
if(i<1||i>L.length+1)
return false;
if(L.length>L.Maxsize)
return false;
for(int j=L.length;j>=i;j++)
L.data[j]=L.data[j-1];
L.data[i-1]=e;
L.length++;
return true;
}
//合并
bool Merge(SeqList A,SeqList B,SeqList &C){
if(A.length+B.length>C.Maxsize)//大于顺序表的最大长度
return false;
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++];
else
C.data[k++]=B.data[j++];
}
while (i < A.length)
C.d