//此为按贪心算法得出的一种算法
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
ifstream fin("C:\\data7.in");
struct region
{
region(int x=0,int y=0):beg(x),end(y){}
region& operator=(const region& inter)
{
beg=inter.beg;
end=inter.end;
return *this;
};
int beg;
int end;
};
int cnt;
int num;
region section[100];
region allowedregion[100];
void sort()
{
for(int i=1;i<cnt;++i)
{
for(int j=i;j>0;--j)
{
region temp;
if(section[j].end<section[j-1].end)
{
temp=section[j];
section[j]=section[j-1];
section[j-1]=temp;
}
else
{
if(section[j].beg<section[j-1].beg)
{
temp=section[j];
section[j]=section[j-1];
section[j-1]=section[j];
}
}
}
}
}
void Init()
{
int beg,end;
cnt=0;
while(fin>>beg>>end)
{
region reg(beg,end);
section[cnt++]=reg;
}
sort();
}
void Chooseregion()
{
int beg,end;
num=0;
for(int i=0;i<cnt;)
{
beg=section[i].beg;
end=section[i].end;
int j;
for(j=i+1;section[j].beg<=end&&j<cnt;++j)
{
if(beg<section[j].beg)
beg=section[j].beg;
}
region inter(beg,end);
allowedregion[num++]=inter;
i=j;
}
}
void print()
{
cout<<"可能区间为:"<<endl;
for(int i=0;i<num;++i)
{
cout<<"pos("<<allowedregion[i].beg<<","<<allowedregion[i].end<<")\t";
}
cout<<endl;
}
int main()
{
Init();
Chooseregion();
print();
system("pause");
return 0;
}
区间选点
最新推荐文章于 2015-01-02 08:40:02 发布