极大点问题

算法分析实验,保存备用,同时供参考交流,如有问题,请提出!

#include<iostream>
#include<vector>
using namespace std;

vector<pair<double, double> > FirstMethod(vector<pair<double,double> > point)
{
	vector<pair<double, double> > maxPoint;//存储极大集点
	vector<pair<double, double> >::iterator it1;
	vector<pair<double, double> >::iterator it2;
	/*找极大点集*/
	for (it1 = point.begin(); it1 != point.end(); it1++)
	{
		/*最后一个点直接加入点集*/
		if(it1 == (point.end()-1))
			maxPoint.push_back(*it1);
		/*循环挨个对比it1后面的所有元素,如果存在y值大的跳出循环,否则it1为极大值点*/
		for (it2 = (it1 + 1); it2 != point.end(); it2++)
		{
			if ((*it2).second >= (*it1).second)
				break;
			else if (it2 == (point.end() - 1))
				maxPoint.push_back(*it1);
		}
	}

	return maxPoint;
}
/*point为输入的平面的点集,num为点的个数*/
vector<pair<double, double> > SecondMethod(vector<pair<double, double> > point , int num)
{

	vector<pair<double, double> > maxPoint;
	vector<pair<double, double> > leftPoint;
	vector<pair<double, double> > rightPoint;
	vector<pair<double, double> > leftMaxPoint;
	vector<pair<double, double> > rightMaxPoint;
	vector<pair<double, double> >::iterator it1;
	vector<pair<double, double> >::iterator it2;
	int a;
	/*如果点集个数为1,直接加入极大值集中*/
	if (num == 1)
		maxPoint.push_back(point[0]);
	else
	{
	/*考虑点集个数为奇数的情况*/
		if (num % 2 == 1)
		{
			for (int i = 0; i < ((num / 2) + 1); i++)//拆分为两部分(左)
			{
				leftPoint.push_back(point[i]);
			}
			for (int j = (num / 2)+1; j < num; j++)//拆分为两部分(右)
			{
				rightPoint.push_back(point[j]);
			}
			leftMaxPoint = SecondMethod(leftPoint, (num / 2)+1);//递归调用左边的点集
			rightMaxPoint = SecondMethod(rightPoint, num / 2);  //递归调用右边的点集
		}
		/*考虑点集个数为偶数的情况*/
		else
		{
			for (int i = 0; i < num / 2; i++)
			{
				leftPoint.push_back(point[i]);
			}
			for (int j = (num / 2); j < num; j++)
			{
				rightPoint.push_back(point[j]);
			}
			leftMaxPoint = SecondMethod(leftPoint, num / 2);
			rightMaxPoint = SecondMethod(rightPoint, num / 2);
		}
		/*将左边点集的极大点集扫描,如果存在y值小于 右边点集的极大集点中y值的,删掉*/
		for (it1 = leftMaxPoint.begin(); it1 != leftMaxPoint.end();)
		{
			a = leftMaxPoint.size();//保存原点集的大小
			for (it2 = rightMaxPoint.begin(); it2 != rightMaxPoint.end(); it2++)
			{
				if ((*it1).second <= (*it2).second)
				{
					it1 = leftMaxPoint.erase(it1);//删除元素,点集大小减1,跳出循环
					break;
				}
			}
			if (leftMaxPoint.size() == a)//没有删除元素
				it1++;
		}
		/*合并所有极大值点*/
		for (int i = 0; i < leftMaxPoint.size(); i++)
		{
			maxPoint.push_back(leftMaxPoint[i]);
		}
		for (int i = 0; i < rightMaxPoint.size(); i++)
		{
			maxPoint.push_back(rightMaxPoint[i]);
		}
	}

	return maxPoint;
}

int main()
{
	vector<pair<double, double> > myMaxPoint1;
	vector<pair<double, double> > myMaxPoint2;
	vector<pair<double, double> > myPoint;
	vector<pair<double, double> >::iterator it1;
	vector<pair<double, double> >::iterator it2;
	
	int num;

	cout << "输入点的个数:" << endl;
	cin >> num;
	myPoint.resize(num);
	cout << "输入所有点:" << endl;
	for (int i = 0; i < num; i++)
	{
		cin >> myPoint[i].first >> myPoint[i].second;
	}

	pair<double, double> temp;
	/*对平面的点按照x的大小升序排列*/
	for (it1 = myPoint.begin(); it1 != myPoint.end(); it1++)
		for (it2 = (it1 + 1); it2 != myPoint.end(); it2++)
		{
			if ((*it2).first < (*it1).first)
			{
				temp = *it1;
				*it1 = *it2;
				*it2 = temp;
			}
		}

	myMaxPoint1 = FirstMethod(myPoint);
	cout << "第一种方法极大点集为:" << endl;

	for (int j = 0; j < myMaxPoint1.size(); j++)
	{
		cout << myMaxPoint1[j].first << "," << myMaxPoint1[j].second << endl;
	}

  	myMaxPoint2 = SecondMethod(myPoint,num);
	cout << "第二种方法的极大点集为:" << endl;

	for (int j = 0; j < myMaxPoint2.size(); j++)
	{
		cout << myMaxPoint2[j].first << "," << myMaxPoint2[j].second << endl;
	}

	return 0;

}


你可能感兴趣的:(极大点问题)