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 题意 你试图把一些多米诺骨牌排成直线,然后推倒它们.但是如果你在放骨牌的时候不小心把刚放的骨牌碰倒了,它就会把相临的一串骨牌全都碰 ...
随机推荐
- 【转】详解JavaScript中的this
ref:http://blog.jobbole.com/39305/ 来源:foocoder 详解JavaScript中的this JavaScript中的this总是让人迷惑,应该是js众所周知的坑 ...
- PHP生成二维码库phpqrcode
Description PHP QR Code is open source (LGPL) library for generating QR Code, 2-dimensional barcode. ...
- appcan weixin 开发
登录微信开放平台:https://open.weixin.qq.com/ 管理中心,创建移动应用,ps:创建应用需要审核,其中 应用包名 需与在线打包安卓时候的 自定义包名一致. 开放平台 应用申请 ...
- java初探native
最近碰见一个java中一个native关键字,不知道是干什么的,如下: public native String FileName(String strURL); static{ ...
- 一月份实现Adb小程序
As Brian said: According to a post on xda-developers, you can enable ADB over WiFi from the device w ...
- [FAQ]String(字串相連)與StringBuilder的差別、原理與優缺點?
原文位於 http://www.dotblogs.com.tw/mis2000lab/archive/2013/09/09/msdn_string_stringbuilder.aspx [FAQ]St ...
- PHP引用文件
require: 可能多次执行的代码用require效率要稍高 require_once: 唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含 include: 语句包含并运行指定 ...
- Telerik XML 数据源绑定的问题
Telerik GridView 默认的 XElement 数据源的直接绑定,会导致内置的sort, filter ,group等功能无法使用. 原因在于Telerik GridView的那些功能是根 ...
- C/C++走过的坑(基础问题篇)
1.有符号int与无符号int比较 #define TOTOL_ELEMENTS (sizeof(a) / sizeof(a[0]) ); int main() { int a[] = {23,24, ...
- CF 191 总结
A. Flipping Game 链接:http://codeforces.com/contest/327/problem/A 题意:从 i 到 j 翻转一次使得 1 的 个数最多~ 直接暴力搞~ # ...