UVA 218

编程入门 行业动态 更新时间:2024-10-27 10:23:05

<a href=https://www.elefans.com/category/jswz/34/1768067.html style=UVA 218"/>

UVA 218

凸包水题

求凸包   求边长

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define eps 1e-8
using namespace std;struct Point
{double x,y;Point (double x = 0,double y = 0):x(x),y(y){}
};typedef Point Vector;Vector operator -(Vector A,Vector B)
{return Vector(A.x-B.x,A.y-B.y);
}double Cross(Vector A, Vector B)
{return A.x*B.y -A.y*B.x;
}int cmp(Point a,Point b)
{if(a.x<b.x)return 1;else if(a.x == b.x && a.y < b.y)return 1;return 0;
}int ConvexHull(Point *s,int n,Point *ch)
{sort(s,s+n,cmp);int m = 0;for(int i = 0; i < n; i++){while(m >1 && Cross(ch[m-1]-ch[m-2],s[i]-ch[m-2])<= 0)m--;ch[m++] = s[i];}int k = m;for(int i = n-2; i >=0; i--){while(m > k && Cross(ch[m-1]-ch[m-2],s[i]-ch[m-2]) <= 0)m--;ch[m++] = s[i];}if(n>1)m--;return m;
}double Length(Point a,Point b)
{return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}int main()
{#ifdef LOCALfreopen("in.txt","r",stdin);#endif // LOCALint N;int T = 1;while(scanf("%d",&N) != EOF){if(N == 0)break;struct Point s[N],t[N];for(int  i = 0; i < N; i++)scanf("%lf%lf",&s[i].x,&s[i].y);int m = ConvexHull(s,N,t);printf("Region #%d:\n",T++);for(int  i = m; i >= 0 ;i--){printf("(%.1lf,%.1lf)",t[i].x,t[i].y);if(i > 0)printf("-");}printf("\n");double dis = 0;for(int i = 0; i < m; i++)dis += Length(t[i],t[(i+1)%m]);printf("Perimeter length = %.2lf\n\n",dis);}return 0;
}


更多推荐

UVA 218

本文发布于:2024-02-13 01:24:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1690169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UVA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!