hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)
Lifting the Stone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4819 Accepted Submission(s): 2006
#include <iostream>
#include <iomanip>
using namespace std;
struct point { double x, y; };
point bcenter(point pnt[], int n){
point p, s;
double tp, area = , tpx = , tpy = ;
p.x = pnt[].x; p.y = pnt[].y;
for (int i = ; i <= n; ++i) { // point: 0 ~ n-1
s.x = pnt[(i == n) ? : i].x;
s.y = pnt[(i == n) ? : i].y;
tp = (p.x * s.y - s.x * p.y); area += tp / ;
tpx += (p.x + s.x) * tp; tpy += (p.y + s.y) * tp;
p.x = s.x; p.y = s.y;
}
s.x = tpx / ( * area); s.y = tpy / ( * area);
return s;
}
point P[];
int main()
{
int T,N;
cin>>T;
cout<<setiosflags(ios::fixed)<<setprecision();
while(T--){
cin>>N;
for(int i=;i<N;i++) //从0开始输入多边形的n个点
cin>>P[i].x>>P[i].y;
point t = bcenter(P,N); //返回重心坐标
cout<<t.x<<' '<<t.y<<endl;
}
return ;
}
重新做了一遍这个题,现在理解了求多边形重心的算法,在poj上找到了一样的题,在两个OJ上同时提交。一开始总是WA,后来看了很多题解和讨论发现是精度问题,由于除法会产生很大的误差,再小的浮点误差,累积到10w后都是很大的误差,所以应该尽量减少除法。我改进之后,算法只有加法和乘法,只有最后再/3。最后在hdu上AC,但是poj上死活AC不了,而且上面使用模板的代码放到poj上竟然超时,百思不得其解。郁闷……
hdu上AC的代码:
#include <stdio.h>
typedef struct {
double x,y;
}Point;
double getS(Point a,Point b,Point c) //返回三角形面积
{
return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y)*(c.x - a.x))/;
}
Point getPZ(Point p[],int n) //返回多边形重心
{
Point z;
double sumx = ,sumy = ;
double sumS = ;
for(int i=;i<=n-;i++){
double S = getS(p[],p[i+],p[i]);
sumS += S;
sumx += (p[].x+p[i].x+p[i+].x)*S;
sumy += (p[].y+p[i].y+p[i+].y)*S;
}
z.x = sumx / (sumS* );
z.y = sumy / (sumS* );
return z;
}
Point p[];
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
Point z = getPZ(p,n);
printf("%.2lf %.2lf\n",z.x,z.y);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)的更多相关文章
- hdu 1115 Lifting the Stone
题目链接:hdu 1115 计算几何求多边形的重心,弄清算法后就是裸题了,这儿有篇博客写得很不错的: 计算几何-多边形的重心 代码如下: #include<cstdio> #include ...
- hdu 1115 Lifting the Stone (数学几何)
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1115 Lifting the Stone 多边形的重心
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Lifting the Stone(求多边形的重心—)
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- poj 1115 Lifting the Stone 计算多边形的中心
Lifting the Stone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- hdu1115 Lifting the Stone(几何,求多边形重心模板题)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1115">http://acm.hdu.edu.cn/showproblem.php ...
- Hdoj 1115.Lifting the Stone 题解
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
- UVALive 4426 Blast the Enemy! --求多边形重心
题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <ios ...
- Lifting the Stone 计算几何 多边形求重心
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
随机推荐
- (转)android适配各种分辨率的问题
Android设备屏幕的尺寸是各式各样的,如小米是4英寸的,Xoom平板是10英寸:分辨率也千奇百怪,800×480,960×540等:Android版本的碎片化问题更是萦绕于心,不过在设计应用时可以 ...
- Java线程-volatile不能保证原子性
下面是一共通过volatile实现原子性的例子: 通过建立100个线程,计算number这个变量最后的结果. package com.Sychronized; public class Volatil ...
- linux sort 、uniq 命令
以文件的每行为单位,从左往右依次按ascii码进行比较 sort sort.txt #默认为升序 -u:去除重复行 sort -u sort.txt -r:降序排列 sort -r sort.txt ...
- 转:sock_ev——linux平台socket事件框架(基于数据报的测试程序) .
上一篇已经做过注释,这一篇直接上代码 /******************************************************************************** ...
- VC、OpenGL、ArcGIS Engine开发的二维三维结合的GIS系统
一.前言 众所周知,二维GIS技术发展了近四十年,伴随着计算机软硬件以及关系型数据库的飞速发展,二维GIS技术已日臻完善.在对地理信息的分析功能上有着无可比拟的优势.一些宏观的地理信息,一维的地理信息 ...
- Mysql导入大SQL文件数据问题
如果sql文件过大,会出现mysql out of memory (Needed XXX bytes) ,或者 "MySQL server has gone away"问题; 另 ...
- 简单hello world
第一步配置路由: 打开app/http/route.php文件,输入:Route::get('/home', 'HomeController@index'); 第二步配置控制器: 控制文件可以手动添加 ...
- Linux 用 shell 脚本 批量 导入 csv 文件 到 mysql 数据库
前提: 每个csv文件第一行为字段名 创建的数据库字段名同csv 文件的字段名 1. 批量导入 多个 csv 文件 for file in ./*.csv;do mv $file tablename. ...
- C复杂声明举例
首先,一些国外的研究成果: 一个用英语解析复杂声明的网站:http://cdecl.org 图表说明复杂声明(英):http://c-faq.com/decl/spiral.anderson.html ...
- C数据结构-栈和队列,括号匹配举例---ShinePans
1.栈和队列是两种特殊的线性表 运算操作被限定仅仅能在表的一端或两端插入,删除元素,故也称它们为限定的线性表结构 2.栈的基本运算 1).Stackinit(&s) 构 ...