首页 > ACM题库 > HDU-杭电 > hdu 2105 The Center of Gravity-计算几何-[解题报告]C++
2013
12-29

hdu 2105 The Center of Gravity-计算几何-[解题报告]C++

The Center of Gravity

问题描述 :

Everyone know the story that how Newton discovered the Universal Gravitation. One day, Newton walked
leisurely, suddenly, an apple hit his head. Then Newton discovered the Universal Gravitation.From then
on,people have sovled many problems by the the theory of the Universal Gravitation. What’s more, wo also
have known every object has its Center of Gravity.
Now,you have been given the coordinates of three points of a triangle. Can you calculate the center
of gravity of the triangle?

输入:

The first line is an integer n,which is the number of test cases.
Then n lines follow. Each line has 6 numbers x1,y1,x2,y2,x3,y3,which are the coordinates of three points.
The input is terminated by n = 0.

输出:

The first line is an integer n,which is the number of test cases.
Then n lines follow. Each line has 6 numbers x1,y1,x2,y2,x3,y3,which are the coordinates of three points.
The input is terminated by n = 0.

样例输入:

2
1.0 2.0 3.0 4.0 5.0 2.0
1.0 1.0 4.0 1.0 1.0 5.0
0

样例输出:

3.0 2.7
2.0 2.3

              题目意思没啥好说的。求三角形的重心。

          套公式就好了。

 

         

#include<cstdio>
#include<cstring>
#include<iostream>
#include<utility>
#include<string>
#include<set>
#include<vector>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;
const int inf=1<<29;
const int M=50200;
const double eps=1e-8;
struct point
{
    double x,y;
}po[5],p,g;
int n;

int main()
{
    int T;
    while(scanf("%d",&T)==1)
    {
        if(!T) break;
        while(T--)
        {
            for(int i=0;i<3;i++)
                scanf("%lf%lf",&po[i].x,&po[i].y);
            double tp,area=0,tpx=0,tpy=0;
            p.x=po[0].x;
            p.y=po[0].y;
            for(int i=1;i<=3;i++)
            {
                g.x=po[(i==3)?0:i].x;
                g.y=po[(i==3)?0:i].y;
                tp=(p.x*g.y-p.y*g.x);
                area+=tp/2;
                tpx=tpx+(p.x+g.x)*tp;
                tpy=tpy+(p.y+g.y)*tp;
                p.x=g.x;
                p.y=g.y;
            }
            g.x=tpx/(6*area);
            g.y=tpy/(6*area);
            printf("%.1lf %.1lf\n",g.x,g.y);
        }
    }
    return 0;
}

 

解题转自:http://blog.csdn.net/new_c_yuer/article/details/6647467


  1. 有两个重复的话结果是正确的,但解法不够严谨,后面重复的覆盖掉前面的,由于题目数据限制也比较严,所以能提交通过。已更新算法

  2. /*
    * =====================================================================================
    *
    * Filename: 1366.cc
    *
    * Description:
    *
    * Version: 1.0
    * Created: 2014年01月06日 14时52分14秒
    * Revision: none
    * Compiler: gcc
    *
    * Author: Wenxian Ni (Hello World~), [email protected]
    * Organization: AMS/ICT
    *
    * =====================================================================================
    */

    #include
    #include

    using namespace std;

    int main()
    {
    stack st;
    int n,i,j;
    int test;
    int a[100001];
    int b[100001];
    while(cin>>n)
    {
    for(i=1;i>a[i];
    for(i=1;i>b[i];
    //st.clear();
    while(!st.empty())
    st.pop();
    i = 1;
    j = 1;

    while(in)
    break;
    }
    while(!st.empty()&&st.top()==b[j])
    {
    st.pop();
    j++;
    }
    }
    if(st.empty())
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    return 0;
    }