标签:des style blog http io ar color os sp
HDU 1115
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5719 Accepted Submission(s): 2391
解法:将多边形切分成n-2个三角形,然后分别算出每个三角形的重心和面积,中心通过三个顶点坐标取得,面积通过叉积算出。
则N边形的重心x坐标为 reultx = (Σ(Si * Pi.x))/S .......Σ是n-2个多边形的面积,Si表示是第i块三角形的面积,Pi
表示第i块三角形的重心的x坐标。同理可得重心y坐标。
由于在计算重心的时候每次都要除以3,则放到最后再除。
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define N 1000010 struct Point { double x,y; Point (){} Point (double x,double y):x(x),y(y){} Point operator - ( Point p){ return Point(x-p.x,y-p.y); } double operator ^ ( Point p){ return x*p.y-y*p.x; } double operator * ( Point p){ return x*p.x+y*p.y; } }; int n; Point p[N]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } double area=0; double sumx=0; double sumy=0; for(int i=2;i<n;i++) { double areaTmp=((p[i]-p[0])^(p[i-1]-p[0]))/2; area+=areaTmp; sumx+=areaTmp*(p[0].x+p[i-1].x+p[i].x); sumy+=areaTmp*(p[0].y+p[i-1].y+p[i].y); } double resultx=sumx/area/3; double resulty=sumy/area/3; //if(resultx==0) resultx=0; //if(resulty==0) resulty=0; 防止输出-0.00 printf("%.2f %.2f\n",resultx,resulty); } return 0; }
NYOJ 3
3 3 0 1 0 2 0 3 3 1 1 0 0 0 1 4 1 1 0 0 0 0.5 0 1
0.000 0.000 0.500 1.000 0.500 1.000
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define N 10010 struct Point { double x,y; Point (){} Point (double x,double y):x(x),y(y){} Point operator - ( Point &p){ return Point(x-p.x,y-p.y); } double operator ^ ( Point &p){ return x*p.y-y*p.x; } double operator * ( Point &p){ return x*p.x+y*p.y; } }; int n; Point p[N]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } double area=0; double sumx=0; double sumy=0; for(int i=2;i<n;i++) { double areaTmp=((p[i]-p[0])^(p[i-1]-p[0]))/2; area+=areaTmp; sumx+=areaTmp*(p[0].x+p[i-1].x+p[i].x); sumy+=areaTmp*(p[0].y+p[i-1].y+p[i].y); } double resultx=sumx/area/3; double resulty=sumy/area/3; if(area==0) resultx=resulty=0; printf("%.3f %.3f\n",area,resultx+resulty); } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/hate13/p/4152302.html