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

poj 3348 Cows

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

poj 3348 Cows

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <cstdlib>using namespace std;struct POINT {int x,y;double angle;}point[10010],stack[10010];int n, top;typedef struct POINT Point;//欧氏距离double Distance(Point p1, Point p2){return sqrt((double)((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)));}//叉积 op_sp × op_epdouble multily(Point sp, Point ep, Point op){return (double)((sp.x - op.x) * (ep.y - op.y) - (ep.x - op.x) * (sp.y - op.y));}//比较函数从小到大int cmp(const void *a, const void *b){Point *c = (Point *)a;Point *d = (Point *)b;if (c->angle > d->angle){return 1;}else if(c->angle < d->angle){return -1;}elsereturn (c->x * c->x + c->y * c->y) < (d->x * d->x + d->y * d->y) ? -1 : 1;}//求凸包void Graham_scan(){int i;stack[0] = point[0];stack[1] = point[1];stack[2] = point[2];for (i = 3; i < n; ++ i){while (multily(point[i], stack[top], stack[top - 1]) > 0){top --;}stack[++ top] = point[i];}}//求出凸包的面积double getArea1(){double sum = 0.0;int i;for (i = 0; i <= top; ++ i){sum += (double)(stack[i].x * stack[(i + 1) % (top + 1)].y - stack[i].y * stack[(i + 1) % (top + 1)].x);}return sum / 2.0;}int main(){double area1;int i, k;while(scanf("%d", &n) != EOF){top = 2;k = 0;for (i = 0; i < n; ++ i){scanf("%d %d", &point[i].x, &point[i].y);}for (i = 1; i < n; ++ i){if (point[i].y < point[k].y || (point[i].y == point[k].y && point[i].x < point[k].x)){k = i;}}if (k){Point tmp = point[0];point[0] = point[k];point[k] = tmp;}for (i = 1; i < n; ++ i){point[i].angle = atan2((double)(point[i].y - point[0].y), (double)(point[i].x - point[0].x));}qsort(point + 1, n - 1, sizeof(point[0]),cmp);Graham_scan();area1 = fabs(getArea1());printf("%dn", (int)(area1/50.0));}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376809.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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