您的位置:首页 > 其它

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

2012-10-03 10:47 260 查看
区间序列 (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);

  }

 }

 

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  浏览器 招聘
相关文章推荐