Lifting the Stone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6971    Accepted Submission(s): 2919

Problem Description
There
are many secret openings in the floor which are covered by a big heavy
stone. When the stone is lifted up, a special mechanism detects this and
activates poisoned arrows that are shot near the opening. The only
possibility is to lift the stone very slowly and carefully. The ACM team
must connect a rope to the stone and then lift it using a pulley.
Moreover, the stone must be lifted all at once; no side can rise before
another. So it is very important to find the centre of gravity and
connect the rope exactly to that point. The stone has a polygonal shape
and its height is the same throughout the whole polygonal area. Your
task is to find the centre of gravity for the given polygon.
 
Input
The
input consists of T test cases. The number of them (T) is given on the
first line of the input file. Each test case begins with a line
containing a single integer N (3 <= N <= 1000000) indicating the
number of points that form the polygon. This is followed by N lines,
each containing two integers Xi and Yi (|Xi|, |Yi| <= 20000). These
numbers are the coordinates of the i-th point. When we connect the
points in the given order, we get a polygon. You may assume that the
edges never touch each other (except the neighboring ones) and that they
never cross. The area of the polygon is never zero, i.e. it cannot
collapse into a single line.
 
Output
Print
exactly one line for each test case. The line should contain exactly
two numbers separated by one space. These numbers are the coordinates of
the centre of gravity. Round the coordinates to the nearest number with
exactly two digits after the decimal point (0.005 rounds up to 0.01).
Note that the centre of gravity may be outside the polygon, if its shape
is not convex. If there is such a case in the input data, print the
centre anyway.
 
Sample Input
2
4
5 0
0 5
-5 0
0 -5
4
1 1
11 1
11 11
1 11
 
Sample Output
0.00 0.00
6.00 6.00
题意:已知一多边形没有边相交,质量分布均匀。顺序给出多边形的顶点坐标,求其重心。
分析:
求多边形重心的题目大致有这么几种:
①,质量集中在顶点上。n个顶点坐标为(xi,yi),质量为mi,则重心
  X = ∑( xi×mi ) / ∑mi
  Y = ∑( yi×mi ) / ∑mi
  特殊地,若每个点的质量相同,则
  X = ∑xi  / n
  Y = ∑yi  / n
②,质量分布均匀。这个题就是这一类型,算法和上面的不同。
  特殊地,质量均匀的三角形重心:
  X = ( x0 + x1 + x2 ) / 3
  Y = ( y0 + y1 + y2 ) / 3
③三角形面积公式:S =  ( (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1) ) / 2 ;  (叉积除二)
因此做题步骤:1、将多边形分割成n-2个三角形,根据③公式求每个三角形面积。  
            2、根据②求每个三角形重心。  
            3、根据①求得多边形重心。 
package 数学题;

import java.text.DecimalFormat;
import java.util.Scanner; public class hdu_1115 {
static class point {
double x, y; point(double x, double y) {
this.x = x;
this.y = y;
}
} static point[] p; public static void main(String[] args) {
DecimalFormat df= (DecimalFormat)DecimalFormat.getInstance();
df.applyPattern("0.00");
Scanner sc = new Scanner(System.in);
int tcase = sc.nextInt();
while (tcase-- > 0) {
int n = sc.nextInt();
p = new point[n];
for (int i = 0; i < n; i++) {
double x = sc.nextDouble();
double y = sc.nextDouble();
p[i] = new point(x, y);
}
double s = 0,sum=0;
double gx = 0,gy=0;
for (int i = 1; i < n - 1; i++) {
s = getArea(p[i], p[i + 1], p[0]);
gx += s * (p[i].x + p[i + 1].x + p[0].x)/3;
gy += s * (p[i].y + p[i + 1].y + p[0].y)/3;
sum+=s;
}
double X = gx / sum;
double Y =gy / sum;
System.out.println(df.format(X)+" "+df.format(Y));
}
}
///叉积除二得面积
private static double getArea(point p1, point p2, point p) {
return ((p1.x - p.x) * (p2.y - p.y) - (p2.x - p.x) * (p1.y - p.y)) / 2;
}
}
 

hdu 1115(多边形重心问题)的更多相关文章

  1. hdu 3685 多边形重心+凸包

    Rotational Painting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. hdu 1115(计算多边形重心)

    题意:已知一多边形没有边相交,质量分布均匀.顺序给出多边形的顶点坐标,求其重心. 分析: 求多边形重心的题目大致有这么几种: 1,质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心 X ...

  4. HDU 1115(求质量均匀分布的多边形重心 物理)

    题意是给一个 n 边形,给出沿逆时针方向分布的各顶点的坐标,求出 n 边形的重心. 求多边形重心的情况大致上有三种: 一.多边形的质量都分布在各顶点上,像是用轻杆连接成的多边形框,各顶点的坐标为Xi, ...

  5. HDOJ(1115)多边形重心

    Lifting the Stone http://acm.hdu.edu.cn/showproblem.php?pid=1115 题目描述:输入n个顶点(整数),求它们围成的多边形的重心. 算法:以一 ...

  6. hdu 1115 Lifting the Stone

    题目链接:hdu 1115 计算几何求多边形的重心,弄清算法后就是裸题了,这儿有篇博客写得很不错的: 计算几何-多边形的重心 代码如下: #include<cstdio> #include ...

  7. Lifting the Stone(多边形重心)

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. hdu1115 Lifting the Stone(几何,求多边形重心模板题)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1115">http://acm.hdu.edu.cn/showproblem.php ...

  9. *HDU 1115 计算几何

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. lintcode-119-编辑距离

    119-编辑距离 给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 插入一个字符 删除一个字符 替换一个字符 样例 给出 work1=&q ...

  2. Hash表 算法的详细解析

    http://xingyunbaijunwei.blog.163.com/blog/static/76538067201111494524190/ 什么是HashHash,一般翻译做“散列”,也有直接 ...

  3. B - 寻找M

    B - 寻找M Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem Descri ...

  4. 11大精选Android自学网站

    无论是从事什么开发,只要是软件行业,不断的更新迭代自己掌握的知识是少不了的.相信干过程序猿的童鞋都清楚,如果要在技术上有所提升,工作之余的不断学习是少不了的.今天小编为大家分享的就是一些比较有用的学习 ...

  5. Java操作Redis存储对象类型数据

    背景描述      关于JAVA去操作Redis时,如何存储一个对象的数据,大家是非常关心的问题,虽然官方提供了存储String,List,Set等等类型,但并不满足我们现在实际应用.存储一个对象是是 ...

  6. MySQL之SELECT 语句详解

    本文参考实验楼的SELECT 语句详解结合自己操作部分而写成. 注意:大多数系统中,SQL语句都是不区分大小写的,但是出于严谨和便于区分保留字和变量名,在书写的时,保留字应大写,而变量名应小写.所谓的 ...

  7. 洛谷 P2168 [NOI2015]荷马史诗 解题报告

    P2168 [NOI2015]荷马史诗 题目描述 追逐影子的人,自己就是影子 --荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷 ...

  8. 常见编程语言对REPL支持情况小结

    最近跟一个朋友聊起编程语言的一些特性,他有个言论让我略有所思:“不能REPL的都是渣”.当然这个观点有点偏激,但我们可以探究一下,我们常用的编程语言里面,哪些支持REPL,哪些不支持,还有REPL的一 ...

  9. gcc用法小记

    By francis_hao    Feb 13,2017 概要 这里只列出了最常用的选项   选项解释 -c|-S|-E 启动gcc编译器时,它会顺序执行预处理.编译.汇编和连接(四个阶段的详细介绍 ...

  10. HDU 1394 Minimum Inversion Number(树状数组/归并排序实现

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...