题目链接:http://acm.uva.es/local/online_judge/search_uva.html

Problem:Find out the center of masses of a convex polygon.

Input:A series of convex polygons, defined as a number n () stating the number of points of the polygon, followed by n different pairs of integers (in no particular order), denoting the x and y coordinates of each point. The input is finished by a fake ``polygon" with m (m < 3) points, which should not be processed.

Output:For each polygon, a single line with the coordinates x and y of the center of masses of that polygon, rounded to three decimal digits.

解法:求多边形的重心。先求出其凸包,然后求重心。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 #define exp 1e-10
 9 #define PI 3.141592654
10 using namespace std;
11 const int maxn=111;
12 struct Point
13 {
14     double x,y;
15     Point (double x=0,double y=0):x(x),y(y){}
16     bool friend operator < (Point a,Point b)
17     {
18         if (a.x!=b.x) return a.x<b.x;
19         return a.y<b.y;
20     }
21 }an[maxn],bn[maxn];
22 typedef Point Vector;
23 Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x , A.y+B.y); }
24 Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }
25 Vector operator * (Vector A,double p) {return Vector(A.x*p , A.y*p); }
26 int dcmp(double x)
27 {
28     if (fabs(x)<exp) return 0;
29     return x<0 ? -1 : 1;
30 }
31 double cross(Vector A,Vector B)
32 {
33     return A.x*B.y-B.x*A.y;
34 }
35 Point PolyGravity(Point *p,int n)
36 {
37     Point tmp,g=Point(0,0);
38     double sumArea=0,area;
39     for (int i=2 ;i<n ;i++)
40     {
41         area=cross(p[i-1]-p[0],p[i]-p[0]);
42         sumArea += area;
43         tmp.x=p[0].x+p[i-1].x+p[i].x;
44         tmp.y=p[0].y+p[i-1].y+p[i].y;
45         g.x += tmp.x*area;
46         g.y += tmp.y*area;
47     }
48     g.x /= (sumArea*3.0);
49     g.y /= (sumArea*3.0);
50     return g;
51 }
52 int ConvexHull(Point *p,int n,Point *ch)
53 {
54     sort(p,p+n);
55     int m=0;
56     for (int i=0 ;i<n ;i++)
57     {
58         while (m>1 && dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<0) m--;
59         ch[m++]=p[i];
60     }
61     int k=m;
62     for (int i=n-2 ;i>=0 ;i--)
63     {
64         while (m>k && dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<0) m--;
65         ch[m++]=p[i];
66     }
67     if (n>1) m--;
68     return m;
69 }
70 int main()
71 {
72     int n;
73     while (scanf("%d",&n)!=EOF)
74     {
75         if (n<3) break;
76         for (int i=0 ;i<n ;i++)
77         {
78             scanf("%lf%lf",&an[i].x,&an[i].y);
79         }
80         int k=ConvexHull(an,n,bn);
81         //cout<<"DEBUG\n";
82         //for (int i=0 ;i<n ;i++) cout<<an[i].x<<" "<<an[i].y<<endl;
83         Point ans=PolyGravity(bn,k);
84         printf("%.3lf %.3lf\n",ans.x,ans.y);
85     }
86     return 0;
87 }

转载于:https://www.cnblogs.com/huangxf/p/3928397.html

UVA 10002 Center of Masses相关推荐

  1. 怎么改vue项目的标题_vue修改项目名

    div自适应高度 div自适应高度 Div即父容器不根据内容自适应高度,我们看下面的代码: sublime 3 主题: Theme: Flatland 着色:todo Blue Dawn.tmThem ...

  2. π-Algorithmist分类题目(2)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(2) Set Theory U ...

  3. 刚体质量分布与牛顿-欧拉方程

    惯性矩.惯性积.转动惯量.惯性张量 惯性矩是一个几何量,通常被用作描述截面抵抗弯曲的性质.惯性矩的国际单位为(m4).即面积二次矩,也称面积惯性矩,而这个概念与质量惯性矩(即转动惯量)是不同概念. 面 ...

  4. uva 167 The Sultan's Successors

    题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108& ...

  5. UVa 10382 - Watering Grass

    链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&pa ...

  6. UVa Online Judge 工具網站

    UVa Online Judge 工具網站 转自http://www.csie.ntnu.edu.tw/~u91029/uva.html Lucky貓的ACM園地,Lucky貓的 ACM 中譯題目 M ...

  7. [搜索]UVa 129 困难的串

    题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...

  8. uva 401.Palindromes

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. Uva 3767 Dynamic len(set(a[L:R])) 树套树

    Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...

  10. UVA 11752 超级幂

    UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

最新文章

  1. java sync和async区别_GCD中串行、并行与async、sync的区别
  2. EF 学习 实用脚本
  3. USACO1.1.2|贪婪的送礼者
  4. opporeno3pro可以刷鸿蒙系统吗,华为P40放大招!鸿蒙系统+120Hz+徕卡五摄,明年2月发布!...
  5. 今天开始记录我的开发生涯
  6. OpenGL--------纹理处理
  7. 谈Dreamweaver和Webstorm
  8. 【李宏毅机器学习视频汇总】2016~2021李宏毅机器学习内容汇总及一些学习建议
  9. 增大或者减小图片大小的方法
  10. 为什么4万月薪招聘不到赴日软件工程师?
  11. logparser使用_分析您的Web服务器数据并使用LogParser和Log Parser Lizard GUI进行授权
  12. cpu和内存占用过高,但是任务管理器中的进程占用的内存和cpu看不出来
  13. 中国知网(cnki)上caj格式转pdf的方法
  14. Java利用JNA调用C#的dll
  15. java 对es的操作
  16. EWM一个仓库号对应ERP多个PLANT的配置
  17. 服务器定时开机设置方法
  18. Foucsky演示工具使用教程入门(笔记)
  19. python from turtle import_import turtle 和 from turtle import * 有什么区别?
  20. JS每日一题:前端电商 sku 全排列的递归回溯算法

热门文章

  1. MVC创建通用DropdownList
  2. 再学C++之C++中的全部关键字
  3. VR打造视觉盛宴,丰富精彩生活
  4. 马云点名的工程师,除了几百封求爱信还有13项区块链专利
  5. Hadoop中maptask数量的决定因素
  6. 学习资料(干货汇集)不断更新【更新于2017-9-17】
  7. 颠覆大数据分析之Storm简介
  8. ASP.NET MVC之文件上传【二】(九)
  9. Java集合与数组实现升序排序的算法设计
  10. UIScrollView总结