题目:http://acm.hit.edu.cn/hoj/problem/view?id=2183
本题核心是求任意多边形的面积。
我们可以用叉积来求。任取一点,顺时针取相邻两点,与这点组成两个向量,进行叉积。顺时针一周求得的所有叉积和求反除以二即是多边形的面积。
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <vector> using namespace std; struct Point { double x; double y; }; Point p[100000]; double cross(Point O,Point A,Point B) { return (A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y); } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif int n; double vol; while(scanf(" %d",&n)!=EOF && n>=3) { p[0].x = p[0].y = 0; for(int i=1;i<=n;i++) { scanf(" %lf %lf",&p[i].x,&p[i].y); } scanf(" %lf",&vol); p[n+1].x = p[1].x; p[n+1].y = p[1].y; double area = 0; for(int i=1;i<=n;i++) { area += cross(p[0],p[i],p[i+1]); } area = -area; area /=2; double len; len = vol/area; printf("BAR LENGTH: %.2lf\n",len); } }
作者:niuox 发表于2013-2-19 23:44:54 原文链接
阅读:88 评论:0 查看评论