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.
解法:求多边形的重心。先求出其凸包,然后求重心。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
#define exp 1e-10
#define PI 3.141592654
using namespace std;
const int maxn=;
struct Point
{
double x,y;
Point (double x=,double y=):x(x),y(y){}
bool friend operator < (Point a,Point b)
{
if (a.x!=b.x) return a.x<b.x;
return a.y<b.y;
}
}an[maxn],bn[maxn];
typedef Point Vector;
Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x , A.y+B.y); }
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }
Vector operator * (Vector A,double p) {return Vector(A.x*p , A.y*p); }
int dcmp(double x)
{
if (fabs(x)<exp) return ;
return x< ? - : ;
}
double cross(Vector A,Vector B)
{
return A.x*B.y-B.x*A.y;
}
Point PolyGravity(Point *p,int n)
{
Point tmp,g=Point(,);
double sumArea=,area;
for (int i= ;i<n ;i++)
{
area=cross(p[i-]-p[],p[i]-p[]);
sumArea += area;
tmp.x=p[].x+p[i-].x+p[i].x;
tmp.y=p[].y+p[i-].y+p[i].y;
g.x += tmp.x*area;
g.y += tmp.y*area;
}
g.x /= (sumArea*3.0);
g.y /= (sumArea*3.0);
return g;
}
int ConvexHull(Point *p,int n,Point *ch)
{
sort(p,p+n);
int m=;
for (int i= ;i<n ;i++)
{
while (m> && dcmp(cross(ch[m-]-ch[m-],p[i]-ch[m-]))<) m--;
ch[m++]=p[i];
}
int k=m;
for (int i=n- ;i>= ;i--)
{
while (m>k && dcmp(cross(ch[m-]-ch[m-],p[i]-ch[m-]))<) m--;
ch[m++]=p[i];
}
if (n>) m--;
return m;
}
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
if (n<) break;
for (int i= ;i<n ;i++)
{
scanf("%lf%lf",&an[i].x,&an[i].y);
}
int k=ConvexHull(an,n,bn);
//cout<<"DEBUG\n";
//for (int i=0 ;i<n ;i++) cout<<an[i].x<<" "<<an[i].y<<endl;
Point ans=PolyGravity(bn,k);
printf("%.3lf %.3lf\n",ans.x,ans.y);
}
return ;
}
UVA 10002 Center of Masses的更多相关文章
- UVA 10585 Center of symmetry
题意:给出一个点集,问这个集合有没有中心点使点集对称,这个点可以是点集中的点也可以不是点集的点. 解法:一开始我枚举每两个点连线的中点……结果T了orz当时也不知道怎么想的…… 将点按横坐标排序,如果 ...
- uva 1444 Knowledge for the masses
uva 1444 Description You are in a library equipped with bookracks that move on rails. There are ma ...
- UVa 1648 (推公式) Business Center
题意: 有一种奇怪的电梯,每次只能向上走u个楼层或者向下走d个楼层 现在有m个这种电梯,求恰好n次能够到达的最小楼层数(必须是正数),最开始默认位于第0层. 分析: 假设电梯向上走x次,则向下走n-x ...
- UVA 1648 Business Center
https://vjudge.net/problem/UVA-1648 设上升x层,列个方程解出来,再把x带回去 #include<cmath> #include<cstdio> ...
- .Uva&LA部分题目代码
1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...
- UVa 11361 - Investigating Div-Sum Property
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 10798 - Be wary of Roses (bfs+hash)
10798 - Be wary of Roses You've always been proud of your prize rose garden. However, some jealous f ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- Dumb Bones UVA - 10529[多米诺重构]
Dumb Bones UVA - 10529 来自绿书p176 题意 你试图把一些多米诺骨牌排成直线,然后推倒它们.但是如果你在放骨牌的时候不小心把刚放的骨牌碰倒了,它就会把相临的一串骨牌全都碰 ...
随机推荐
- Setup Factory 程序打包
1.检测是否已安装 在On Startup中写入如下代码: result1=Registry.DoesKeyExist(HKEY_LOCAL_MACHINE, "Software\\sohe ...
- php验证是否是md5编码的代码
php验证是否是md5编码的示例. 代码很简单,使用了正则表达式. function is_md5($password) { return preg_match("/^[a-z0-9 ...
- C++判断五位以内的对称素数
题目内容:判断一个数是否为对称且不大于五位数的素数. 输入描述:输入数据含有不多于50个的正整数n(0<n<232). 输出描述:对于每个n,如果该数是不大于五位数的对称素数,则输出“Ye ...
- ASP.NET MVC5学习笔记之Action参数模型绑定基本过程
当我们在Controller中定义一个Action,通常会定义一个或多个参数,每个参数称为一个模型,ASP.NET MVC框架提供了一种机制称为模型绑定,会尝试自动从请求的信息中实例化每一个模型并赋值 ...
- EF经验分享_jimmyzzc
刚刚接触EF,总结了一些在实际工作中的心德经验,与大家分享一下. 一.Excression表达式树 表达式目录树在LINQ中用于表示分配给类型为Expression<TDelegate>的 ...
- xtrabackup之Innobackupex全备恢复
一.当前环境 [mysql@hadoop1 ~]$ mysql --defaults-/my.cnf -uroot -p123456 -P3306 mysql> show variables l ...
- EMVTag系列15《选择应用响应数据》
1. 接触交易选择应用响应数据 标签 长度 数据域 9102 A5 变长 FCI专用模板 强制 50 1–16 应用标签 纯电子现金:PBOC DEBIT 借记卡:PBOC DEBIT 贷记卡:PBO ...
- 网站网页生成.shtml访问无法显示
网站换了服务器后发现shtml网页无法访问,原因是没有注册.shtml扩展名,解决方法如下 IIS6.0解析shtm,shtml文件由于IIS6.0的安全性较以前有特别大的改进,所以在很多功能默认情况 ...
- Oracle Database Gateway 安装
在[Oracle HS (Heterogeneous Services)深入解析 及协同Gateway工作流程]一文中主要主要介绍了HS的工作原理,及其如何协同Gateway一起工作.那么了解Gate ...
- Erlang generic standard behaviours -- gen
在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...