UVA 10002 Center of Masses
题目链接: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相关推荐
- 怎么改vue项目的标题_vue修改项目名
div自适应高度 div自适应高度 Div即父容器不根据内容自适应高度,我们看下面的代码: sublime 3 主题: Theme: Flatland 着色:todo Blue Dawn.tmThem ...
- π-Algorithmist分类题目(2)
原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(2) Set Theory U ...
- 刚体质量分布与牛顿-欧拉方程
惯性矩.惯性积.转动惯量.惯性张量 惯性矩是一个几何量,通常被用作描述截面抵抗弯曲的性质.惯性矩的国际单位为(m4).即面积二次矩,也称面积惯性矩,而这个概念与质量惯性矩(即转动惯量)是不同概念. 面 ...
- uva 167 The Sultan's Successors
题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108& ...
- UVa 10382 - Watering Grass
链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&pa ...
- UVa Online Judge 工具網站
UVa Online Judge 工具網站 转自http://www.csie.ntnu.edu.tw/~u91029/uva.html Lucky貓的ACM園地,Lucky貓的 ACM 中譯題目 M ...
- [搜索]UVa 129 困难的串
题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...
- uva 401.Palindromes
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 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 ...
- UVA 11752 超级幂
UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & ...
最新文章
- java sync和async区别_GCD中串行、并行与async、sync的区别
- EF 学习 实用脚本
- USACO1.1.2|贪婪的送礼者
- opporeno3pro可以刷鸿蒙系统吗,华为P40放大招!鸿蒙系统+120Hz+徕卡五摄,明年2月发布!...
- 今天开始记录我的开发生涯
- OpenGL--------纹理处理
- 谈Dreamweaver和Webstorm
- 【李宏毅机器学习视频汇总】2016~2021李宏毅机器学习内容汇总及一些学习建议
- 增大或者减小图片大小的方法
- 为什么4万月薪招聘不到赴日软件工程师?
- logparser使用_分析您的Web服务器数据并使用LogParser和Log Parser Lizard GUI进行授权
- cpu和内存占用过高,但是任务管理器中的进程占用的内存和cpu看不出来
- 中国知网(cnki)上caj格式转pdf的方法
- Java利用JNA调用C#的dll
- java 对es的操作
- EWM一个仓库号对应ERP多个PLANT的配置
- 服务器定时开机设置方法
- Foucsky演示工具使用教程入门(笔记)
- python from turtle import_import turtle 和 from turtle import * 有什么区别?
- JS每日一题:前端电商 sku 全排列的递归回溯算法