要求使用的排序规则:
点按照离原点从近到远排。距离相同,则按x坐标从小到大排。x坐标也相同,则按y坐标从小到大排。
示例:
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point{
int x;
int y;
};
struct Rule1//自定义规则
{
bool operator()(const Point& ps1,const Point&ps2)const
{if( ps1.x*ps1.x+ps1.y*ps1.y !=ps2.x*ps2.x+ps2.y*ps2.y )//求坐标到原点的距离
return ps1.x*ps1.x+ps1.y*ps1.y<ps2.x*ps2.x+ps2.y*ps2.y;//按坐标离原点由近及远排序
else//坐标到原点的距离相等的情况下
if(ps1.x!=ps2.x)
return ps1.x<ps2.x;//按x坐标由小到大排序
else//坐标到原点的距离相等且x坐标相同的情况下
return ps1.y<ps2.y;//按y坐标由小到大排序
}
};
int main()
{
Point ps[8] = {
{1,0},{0,1},{0,-1},{-1,0},{1,-1},{1,1},{2,0},{-2,0}};
sort(ps,ps+8,Rule1());
for(int i=0;i< 8;