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


    Lifting the Stone

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 230 Accepted Submission(s): 130
     
    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
     
     
    Source
    Central Europe 1999
     
    Recommend
    Eddy
     
    /*
    给你一个多边形,然后让你求多边形的重心 
    
    初步思路:听了蒋金讲了一个重心加权平均求总重心,就是n边形,分割成n-2个小三角形,然后求出重心,面积;
    用公式  求和Si*(Xi,Yi)/Sall   求出n边形的重心
    
    #错误:加权平局的时候莫名地错
    
    #错误反思点:double卡的精度问题,中间过程尽量不要出现先除又乘的问题,因为那样有double精度的问题会产生误差。
    
    1e6的数据量只能遍历一边
    */
    #include<bits/stdc++.h>
    #define N 1000010
    using namespace std;
    struct Point{
        double x,y;
        Point(){}
        Point(double a,double b){
            x=a;
            y=b;
        }
        void input(){
            scanf("%lf%lf",&x,&y);
        }
    };
    Point p;
    int t,n;
    vector<Point>v;//用来存储所有的点
    vector<Point>Focus;//存放每个小三角形的重心
    double s[N];//用来存放n-2个小三角形的面积
    void init(){
        v.clear();
        Focus.clear();
    }
    double dis(Point a,Point b){//两点间距离
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }    
    Point operation_Focus(){
        double sall=0;
        Point p(0,0);
        for(int i=1;i<v.size()-1;i++){//每个三角形由v[0],v[i],v[i+1]三个顶点组成
            //面积
            s[i-1]=(v[i].x - v[0].x)*(v[i+1].y - v[0].y) - (v[i].y-v[0].y)*(v[i+1].x - v[0].x);
            sall+=s[i-1];
            //cout<<"S="<<s[i-1]<<endl;
            //重心
            p.x+=s[i-1]*(v[i].x+v[i+1].x+v[0].x)*1.0/3;  
            p.y+=s[i-1]*(v[i].y+v[i+1].y+v[0].y)*1.0/3;
            //cout<<"Point=("<<(fx*2+v[0].x)/3<<","<<(fy*2+v[0].y)/3<<")"<<endl;
        }
        p.x/=sall*1.0;
        p.y/=sall*1.0;
        return p;
    }
    int main(){
        //freopen("in.txt","r",stdin);
        scanf("%d",&t);
        //cout<<t<<endl;
        while(t--){
            scanf("%d",&n);
            //cout<<n<<endl;
            init();
            for(int i=0;i<n;i++){
                p.input();
                //cout<<p.x<<" "<<p.y<<endl;
                v.push_back(p);
            }//将所有的点存入vector
            p=operation_Focus();
            printf("%.2f %.2f
    ",p.x,p.y);
        }
        return 0;
    }
  • 相关阅读:
    移动端网页 -- 安卓与IOS兼容
    item上下自动循环滚动显示
    刮刮奖 --- 可以自定义在图层下添加文字等等信息
    内核终端判断,微信?QQ?ipad?IE?移动?Google?opera……
    flexbox布局
    刮刮奖
    constructor
    获取地址栏参数
    this对象
    函数
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6219469.html
Copyright © 2020-2023  润新知