程序无法运行,但是找不出答案来
这是我的程序,无法运行直接跳转到stl中去,哪里少写了或多写了
下面代码使求凸包的题目,不是sort的问题
[code=C/C++][/code]#include<stdio.h>
#include <iostream>
#include<algorithm>
#include<math.h>
const double PI=acos(-1.0);
using namespace std;
typedef struct
{
int x,y;
bool operator < ( Point l,Point r)
{
return l.y<r.y || (l.y==r.y && l.x<r.x);
}
}Point;
bool det(Point p1,Point p2,Point p3)
{
return (p1.x-p3.x)*(p2.y-p3.y)>=(p2.x-p3.x)*(p1.y-p3.y);
}
int graham(Point pnt[],int n,Point res[])
{
int i,len,top=1;
sort(pnt,pnt+n);
if(n==0) return 0;
res[0]=pnt[0];
if(n==1) return 1;
res[1]=pnt[1];
if(n==2) return 2;
res[2]=pnt[2];
for (i=2; i<n; i++)
{
while(top && det(pnt[i],res[top],res[top-1])) top--;
res[++top]=pnt[i];
}
len=top;
res[++top]=pnt[n-2];
for (i=n-3; i>=0; i--)
{
while(top!=len && det(pnt[i],res[top],res[top-1])) top--;
res[++top]=pnt[i];
}
return top;
}
double distance(Point p1,Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main()
{
int N,L,i;
Point node[1011],res[1011];
while(scanf("%d%d",&N,&L)!=EOF)
{
for (i=0; i<N; i++)
scanf("%d%d",&node[i].x,&node[i].y);
int len=graham(node,N,res);
double dis;
for (i=0; i<=len; i++)
{
dis+=distance(res[i],res[(i+1)%len]);
}
printf("%.0lf",dis+PI*L*2);
}
return 0;
}
[解决办法]
本人没学过算法,不知道啥是包包,给你把程序改了下,比如一些重定义的问题,或者传参时一些影响效率的东东。
现在是这样,编译能通过了:
#include <iostream>
#include <algorithm>
#include <math.h>
const double PI=acos(-1.0);
using namespace std;
class Point
{
public:
int x,y;
bool operator < (const Point& r) const
{
return this->y<r.y || (this->y==r.y && this->x<r.x);
}
};
bool det(const Point& p1, const Point& p2, const Point& p3)
{
return (p1.x-p3.x)*(p2.y-p3.y)>=(p2.x-p3.x)*(p1.y-p3.y);
}
int graham(Point* pnt, int n, Point* res)
{
int i,len,top=1;
sort(pnt,pnt+n);
if(n==0) return 0;
res[0]=pnt[0];
if(n==1) return 1;
res[1]=pnt[1];
if(n==2) return 2;
res[2]=pnt[2];
for (i=2; i<n; i++)
{
while(top && det(pnt[i],res[top],res[top-1])) top--;
res[++top]=pnt[i];
}
len=top;
res[++top]=pnt[n-2];
for (i=n-3; i>=0; i--)
{
while(top!=len && det(pnt[i],res[top],res[top-1])) top--;
res[++top]=pnt[i];
}
return top;
}
double _distance(const Point& p1, const Point& p2)
{
return sqrt(double((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}
int main()
{
int N,L,i;
Point node[1011],res[1011];
while(scanf("%d%d",&N,&L)!=EOF)
{
for (i=0; i<N; i++)
scanf("%d%d",&node[i].x,&node[i].y);
int len=graham(node,N,res);
double dis;
for (i=0; i<=len; i++)
{
dis += _distance(res[i],res[(i+1)%len]);
}
printf("%.0lf",dis+PI*L*2);
}
return 0;
}
结果就不知道了,自己改改吧。