Lifting the Stone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4214 Accepted Submission(s): 1738
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1115
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 个点的坐标, 求 n 个点构成的多边形的重心。
解题思路:
链接 :
http://www.cnblogs.com/Duahanlang/archive/2013/05/12/3074339.html 此处有详细介绍,我就不多赘言。
ac 代码:
View Code
1 #include<iostream> 2 #include<cmath> 3 #include<cstdio> 4 #include<cstring> 5 6 using namespace std; 7 8 int main() 9 { 10 int n, i, j, t; 11 double A ; //有向面积 12 double vs; //各小三角形有向面积 13 double cx , cy ; //重心的位置坐标 14 double x1, y1, x2, y2, x3, y3; 15 cin >> t; 16 while(t--) { 17 cx = 0, cy = 0, A = 0; 18 cin >> n; 19 cin >> x1 >> y1 >> x2 >> y2; 20 for(i = 2; i < n; ++i) { 21 cin >> x3 >> y3; 22 vs = (x2-x1) * (y3-y1) - (x3-x1) * (y2-y1); //有向面积 23 A += vs; 24 cx += (x1+x2+x3)*vs, cy += (y1+y2+y3)*vs; 25 x2 = x3, y2 = y3; 26 } 27 cx /= (3.0*A), cy /= (3.0*A); 28 printf("%.2lf %.2lf\n", cx, cy); 29 } 30 return 0; 31 }