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-2个三角形,求每个三角形的重心,并以三角形面积为权重,求出各重心坐标的加权平均值,就是多边形的重心

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct point{
double x,y;
point operator - (point rhs){return (point){x-rhs.x,y-rhs.y};}
point operator + (point rhs){return (point){x+rhs.x,y+rhs.y};}
}p[mxn];
double Cross(point a,point b){return a.x*b.y-a.y*b.x;}
int n;
double smm;
double SX,SY;
int main(){
int i,j;
int T=read();
while(T--){
smm=SX=SY=;
n=read();
for(i=;i<=n;i++){
p[i].x=read();p[i].y=read();
}
// p[n+1]=p[1];
for(i=;i<n;i++){
double tmp=Cross(p[i]-p[],p[i+]-p[]);
SX+=(p[].x+p[i+].x+p[i].x)*tmp;
SY+=(p[].y+p[i+].y+p[i].y)*tmp;
smm+=tmp;
}
printf("%.2f %.2f\n",SX/smm/,SY/smm/);
}
return ;
}

POJ1385 Lifting the Stone的更多相关文章

  1. POJ1385 Lifting the Stone 多边形重心

    POJ1385 给定n个顶点 顺序连成多边形 求重心 n<=1e+6 比较裸的重心问题 没有特别数据 由于答案保留两位小数四舍五入 需要+0.0005消除误差 #include<iostr ...

  2. poj 1115 Lifting the Stone 计算多边形的中心

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

  3. Lifting the Stone(hdoj1115)

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

  4. (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 ...

  5. Lifting the Stone(求多边形的重心—)

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

  6. hdu 1115 Lifting the Stone 多边形的重心

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

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

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

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

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

  9. hdu 1115 Lifting the Stone (数学几何)

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

随机推荐

  1. 多种语言书写 “ HelloWorld ”

    最基本的C: #include<stdio.h> int main(int argc, char const *argv[]) { printf("HelloWorld\n&qu ...

  2. org.hibernate.hql.internal.ast.QuerysyntaxException:user is not mapped [from User where user_code=? and user_password=?]

    初学者,一个很低级的错误吧! 找不到映射,最后发现没把类的Hibernate映射文件 添加到Hibernate核心配置文件中去,所以报了这个异常! 在核心文件中添加映射   <mapping r ...

  3. 分享一个漂亮按钮插件FancyButtons

    一转眼,2018年的第10天就这样过去了.回看17年,曾经做了些啥都忘记了,就像每一天写日志时的样子(双手放在键盘上,怒着嘴,抬着头,望着天花板), 然后突然记得好像好久没有写随笔了(@_@).自从配 ...

  4. 【前端_js】ajax的应用

    1.设置请求头部 function makeRequest() { alert("inside makeRequest()"); var settings = { type: &q ...

  5. jsp--提交表单→插入数据库→成功后返回提示信息

    <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="u ...

  6. jupyter notebook(三)——IOPub_data_rate_limit报错

    一.问题 运行jupyter notebook,然后运行python代码,读取文件处理时,会报错.发现时IO读取时错误.应该是IO速率问题. 下面是问题报错: IOPub data rate exce ...

  7. CentOS下的Redis启动脚本

    这是一个Shell脚本,用于管理Redis进程(启动,停止,重启),如果你在使用Redis,这个脚本可供参考. #!/bin/sh # # redis - this script starts and ...

  8. android静默安装和智能安装(转)

    Android 静默安装和智能安装的实现方法 http://blog.csdn.net/fuchaosz/article/details/51852442 Android静默安装实现方案,仿360手机 ...

  9. 「微信小程序免费辅导教程」25,基本内容组件text的使用及个人帐号允许的服务类目

  10. 03014_EL技术

    1.EL表达式概述 EL(Express Language)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL出现的目的是要替代jsp页面中脚本的编写. 2.EL从域中取出数据(EL最重要的作 ...