栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1453 Surround the Trees

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1453 Surround the Trees

#include <stdio.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <math.h>using namespace std;const int MAX = 120;typedef struct Point{double x,y;}Point;Point c[MAX];double crossProduct(Point a,Point b,Point c){return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);}bool cmp(Point a,Point b){if( a.y == b.y )return a.x < b.x;return a.y < b.y;}bool cmp1(Point a,Point b){return crossProduct(c[0],a,b) < 0 ? 1 : 0;}double Distance(Point a,Point b){return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );}Point stk[MAX];int top;double dis(){double sum = 0.0;for(int i=0; i<top; i++)sum += Distance(stk[i],stk[i+1]);sum += Distance(stk[top],stk[0]);return sum;}double Graham(int n){sort(c,c+n,cmp);sort(c+1,c+n,cmp1);top = 0;stk[top++] = c[0];stk[top++] = c[1];stk[top++] = c[2];top--;for(int i=3; i<n; i++){while(1){Point a,b;a = stk[top];b = stk[top-1];if( crossProduct(a,b,c[i]) < 0 )top--;elsebreak;}stk[++top] = c[i];}return dis();}int main(){int n;while( ~scanf("%d",&n) && n ){for(int i=0; i<n; i++)scanf("%lf %lf",&c[i].x,&c[i].y);if( n == 1 ){printf("0.00n");continue;}if( n == 2 ){printf("%.2lfn",2*Distance(c[0],c[1]));continue;}double ans = Graham(n);printf("%.2lfn",ans);}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号