RT.给定若干区间求其并集实现。
#include <iostream>
#define NULL 0
using namespace std;
struct section//每个小区间
{
double start;
double end;
bool isuse;
};
struct whole_section//区间并集
{
section* sec;
int length;
int size;
};
void init(whole_section* ws)//初始化区间集
{
if((ws->sec=(section*)malloc(50*sizeof(section)))==NULL)
return ;
ws->size=50;
ws->length=0;
}
void insert(whole_section* ws,double tstart,double tend)//插入
{
if(tstart>=tend)
return ;
int i;
bool flag=false;
for(i=0;i<ws->length;i++)
{
if(ws->sec[i].isuse==false)//如果是已经不用的小区间则跳过
continue;
if(tstart<ws->sec[i].start)//分为六种情况
{
if(tend<ws->sec[i].start)
{
continue;
}
else if(tend<=ws->sec[i].end)
{
ws->sec[i].start=tstart;
flag=true;
break;
}
else
{
ws->sec[i].start=0;