hdu 1115 Lifting the Stone
题目链接:hdu 1115
计算几何求多边形的重心,弄清算法后就是裸题了,这儿有篇博客写得很不错的: 计算几何-多边形的重心
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = ; struct point {
double x,y;
point() {}
point(double x, double y): x(x), y(y) {}
void read() { scanf("%lf %lf",&x,&y); }
void readint() {
int x,y;
scanf("%d %d",&x,&y);
this->x = x;
this->y = y;
}
point operator - (const point &p2) const {
return point(x - p2.x, y - p2.y);
}
} p[N]; typedef point Vector; double cross(Vector a, Vector b) {
return a.x * b.y - a.y * b.x;
} double Area(point a, point b, point c) {
return cross(b - a, c - a) / ;
} inline double center3(double a, double b, double c) {
return a + b + c;
} int main() {
int t,n;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
for(int i = ; i <= n; ++i)
p[i].read(); double up = , down = ;
point ans;
for(int i = ; i <= n - ; ++i) {
up += center3(p[].x, p[i].x, p[i + ].x) * Area(p[], p[i], p[i + ]);
down += Area(p[], p[i], p[i + ]);
}
ans.x = up / down / ; up = ; down = ;
for(int i = ; i <= n - ; ++i) {
up += center3(p[].y, p[i].y, p[i + ].y) * Area(p[], p[i], p[i + ]);
down += Area(p[], p[i], p[i + ]);
}
ans.y = up / down / ;
printf("%.2lf %.2lf\n", ans.x, ans.y);
}
return ;
}
有个要注意的小细节,对每个小三角形求重心时对最后结果 / 3 即可,而不必在中间过程 / 3,应该是精度问题,所以除法应尽可能避免,因为这题的 n 有点大,所以不断 / 3 操作损失的精度是很大的。
hdu 1115 Lifting the Stone的更多相关文章
- 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 ...
- hdu 1115 Lifting the Stone (数学几何)
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- poj 1115 Lifting the Stone 计算多边形的中心
Lifting the Stone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- Hdoj 1115.Lifting the Stone 题解
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
- hdu1115 Lifting the Stone(几何,求多边形重心模板题)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1115">http://acm.hdu.edu.cn/showproblem.php ...
- (hdu step 7.1.3)Lifting the Stone(求凸多边形的重心)
题目: Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Lifting the Stone(多边形重心)
Lifting the Stone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- *HDU 1115 计算几何
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- c# 反射列子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- C#常用扩展方法
/// <summary> /// 转换 /// </summary> public static class ConversionHelper { #region 数据格式转 ...
- java中|与||,&与&&到底有什么区别呢?
&是位运算符.&&是布尔逻辑运算符.在运行上,&两边的条件都要判断(不管前面的是ture还是false),而&&先判断前面的,若为false,则后面的不 ...
- Data
[pdf你真可爱] [题目分析] 上午考试想到用二分答案做,写残了... 设两个数列,a和b,a表示磁头,看作指针,b就是要扫描的那个序列. 假设一个答案mid,就是a中的数字走mid步能否到达b中的 ...
- MVC4 经典增删改查详情demo
MVC4 经典增删改查详情demo 源码 不解释 Mvc4增删改查详情Demo.7z public ActionResult Detail(int? id) { ViewData.Model ...
- 腾讯微博模拟登陆+数据抓取(java实现)
不多说,贴出相关代码. 参数实体: package token.def; import java.io.Serializable; import java.util.Properties; publi ...
- mysql计划任务
这两天一直遇见mysql计划任务的案例,今天我就给大家分享一个真是的实例: 1.创建计划任务的语法: create event 任务名称 on schedule at 时间周期 starts '年- ...
- Duilib将UI资源文件打包到exe教程
转载:http://www.voidcn.com/blog/w839687571/article/p-6001921.html 转载:http://www.voidcn.com/blog/x35698 ...
- QT笔记之QLineEdit自动补全以及控件提升
转载:http://www.cnblogs.com/csuftzzk/p/qss_lineedit_completer.html?utm_source=tuicool&utm_medium=r ...
- JavaScript的循环语句
JavaScript的循环语句 1.JavaScript的循环语句 (1)for循环语句 - 循环代码块一定的次数: (2)for/in循环语句 - 循环遍历对象的属性: (3)while循环语句 - ...