百度百科里找的凸包代码:
#include<iostream>
#include<algorithm>
using namespace std;
struct point
{
int x;
int y;
}p[30005],res[30005];
int cmp(point p1,point p2)
{
return p1.y<p2.y||(p1.y==p2.y&&p1.x<p2.x);
}
bool ral(point p1,point p2,point p3)
{
if((p2.x-p1.x)*(p3.y-p1.y)>(p3.x-p1.x)*(p2.y-p1.y))
return true;return false;
}
int main()
{
int n,i,j;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>p[i].x>>p[i].y;
if(n==1)
{
cout<<&p[0].x<<&p[0].y;
continue;
}
if(n==2)
{
cout<<&p[0].x<<&p[0].y;
cout<<&p[1].x<<&p[1].y;
continue;
}
sort(p,p+n,cmp);
res[0]=p[0];
res[1]=p[1];
int top=1;
for(i=2;i<n;i++)
{
while(top&&!ral(res[top],res[top-1],p[i]))
top--;
res[++top]=p[i];
}
int len=top;
res[++top]=p[n-2];
for(i=n-3;i>=0;i--)
{
while(top!=len&&!ral(res[top],res[top-1],p[i]))
top--;
res[++top]=p[i];
}
for(i=0;i<top;i++)
{
cout<<res[i].x<<res[i].y;
cout<<endl;
}
}
}
#include<iostream>
#include<algorithm>
using namespace std;
struct point
{
int x;
int y;
}p[30005],res[30005];
int cmp(point p1,point p2)
{
return p1.y<p2.y||(p1.y==p2.y&&p1.x<p2.x);
}
bool ral(point p1,point p2,point p3)
{
if((p2.x-p1.x)*(p3.y-p1.y)>(p3.x-p1.x)*(p2.y-p1.y))
return true;return false;
}
int main()
{
int n,i,j;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>p[i].x>>p[i].y;
if(n==1)
{
cout<<&p[0].x<<&p[0].y;
continue;
}
if(n==2)
{
cout<<&p[0].x<<&p[0].y;
cout<<&p[1].x<<&p[1].y;
continue;
}
sort(p,p+n,cmp);
res[0]=p[0];
res[1]=p[1];
int top=1;
for(i=2;i<n;i++)
{
while(top&&!ral(res[top],res[top-1],p[i]))
top--;
res[++top]=p[i];
}
int len=top;
res[++top]=p[n-2];
for(i=n-3;i>=0;i--)
{
while(top!=len&&!ral(res[top],res[top-1],p[i]))
top--;
res[++top]=p[i];
}
for(i=0;i<top;i++)
{
cout<<res[i].x<<res[i].y;
cout<<endl;
}
}
}
