区间序列 (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);
}
}
}
算法:
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);
}
}
}
相关文章推荐
- 【100题】给定入栈序列,判断一个序列是否可能为输出序列
- 给定一个整数(字符串也适用)序列,重新排列,得到最大的结果,比如31,3,35,7,9根据排列后得到组合 9735331,返回的是string类型
- 给定一个整数判断位数并实现分别输出每位,逆序输出。
- 判断一个序列是否是另一个给定序列的弹出序列
- 小算法:一个排名区间,对应一个奖品,给定一个排名区间,判断它要领取奖品的id
- 判断一个序列是否为栈的输出序列
- hdu6003 Problem Buyer 贪心 给定n个区间,以及m个数,求从n个区间中任意选k个区间,满足m个数都能在k个区间中找到一个包含它的区间,如果一个区间包含了x,那么 该区间不能再去包含另一个数,即k>=m。求最小的k。如果不存在这样的k,输出“IMPOSSIBLE!”。
- 给定一个字符串,输出最长的重复子序列
- 判断给定序列是否为某BST后序输出
- 给定BST先序遍历序列,不构造BST的情况下判断BST是否每个node都只有一个child
- 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZde
- 6-3 判断满足条件的三位数 本题要求实现一个函数,统计给定区间内的三位数中有两位数字相同的完全平方数(如144、676)的个数。
- 有一个队列 ABCDEFG, 现有一个堆栈, 有三种操作, 出队, 入栈,出栈. 输出可以是出队操作也可以是出栈操作.试问如何判断某一序列不是无法输出的?
- 输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。为了简单起见,我们假设push序列的任意两个整数都是不相等的。 比如输入的push序列是1、2、3、4、5,那么4、5、3、2、1就有可能是一个pop系列。
- 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZde
- 给定入栈序列,判断一个序列是不是出栈序列
- 原有一组数据区间段,给定一个区间,判断此区间是否和其他区间相交
- 给定一个数组,判断这个数组的元素是否是在一个序列
- [置顶] 【面试题】判断一个序列是不是栈的输出序列