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;
}
}
 

最新文章

  1. 获取移除指定Url参数(原创)
  2. (原创)VM中的CentOS6.4中安装CloudStack6.3①
  3. Java-数组练习4
  4. Oracle数据库建表并用SQL编程分等级
  5. C++中const 的各种用法
  6. WPF 实现带标题的TextBox
  7. 万向节死锁 gimbal lock
  8. struts2.0的工作原理
  9. VC++6.0环境下调试c语言代码的方法和步骤_附图
  10. BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演
  11. Android使用 selector 自定义控件背景 (以spinner 为例)
  12. Codeforces548D:Mike and Feet(单调栈)
  13. Jmeter(1)介绍
  14. 多重背包--java
  15. SqlServer中常常搞不清楚 sp_columns来看一看
  16. DFT,DTFT,DFS,FFT区别
  17. 前端构建和模块化工具-coolie
  18. [转]使用C#开发ActiveX控件全攻略
  19. MongoDB怎么用?
  20. apk文件签名绕过

热门文章

  1. python基础之删除文件及删除目录的方法
  2. 【Python】Python内置函数dir详解
  3. STL Allocator
  4. xml解析标签
  5. CSS clear both清除浮动总结
  6. [NOIP2017 TG D2T2]宝藏
  7. ZOJ 3496 Assignment | 二分+有上下界网络流
  8. CF858F Wizard&#39;s Tour 解题报告
  9. Android中使用RadioButton代替ImageButton
  10. Angular白名单&amp;&amp;Angular拦截器 全局通用