区间序列 (2,3) (4.2,6) (7,9),判断给定的一个区间与上面哪些区间有交集,比如给定(4,5)则输出(4.2,6)。

本文介绍了一种用于检测给定区间与多个已知区间序列是否有交集的算法。通过迭代器遍历区间序列,并利用特定条件判断交集的存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

区间序列 (2,3) (4.2,6) (7,9),判断给定的一个区间与上面哪些区间有交集,比如给定(4,5)则输出(4.2,6)。

 

算法:

1)首先将测试的区间序列放入迭代器;

2)判断给定的区间是否和测试的区间有交集;判断的方法是如下:

 

#include "stdafx.h"
#include "stdlib.h"
#include <string>

#include <vector>
#include <list>
#include <algorithm>

using namespace std;

 

typedef struct {
 float low;
 float high;
 bool interflag; //是否和特定的区间段相交
}section;

 

void InterSection(vector<section> &sec,const section &dst)
{

 std::vector<section>::iterator iter;

 for (iter=sec.begin();iter!=sec.end();iter++)
 {

  if (!((iter->high<dst.low) ||(iter->low>dst.high)))
  {
   iter->interflag=true;
  }
 }

 return;
}

 


int main(void)
{


 vector<section>SectionList;    //保存所有的区间


 section TmpSection;
 TmpSection.low=2;
 TmpSection.high=3;
 TmpSection.interflag=false;

 SectionList.push_back(TmpSection);

 TmpSection.low=(float)4.2;
 TmpSection.high=6;
 TmpSection.interflag=false;

 SectionList.push_back(TmpSection);

 TmpSection.low=7;
 TmpSection.high=9;
 TmpSection.interflag=false;

 SectionList.push_back(TmpSection);


 TmpSection.low=4;
 TmpSection.high=5;
 TmpSection.interflag=false;

 
 InterSection(SectionList,TmpSection);

 vector<section>::iterator iter;

 for (iter=SectionList.begin();iter!=SectionList.end();iter++)
 {
  if (iter->interflag)
  {
   printf("inter section with [%f,%f]\n",iter->low,iter->high);
  }
 }

 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值