题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2036
Problem Description “ 改革春风吹满地,
Input 输入数据包含多个测试实例,每个测试实例占一行,每行的开始是一个整数n(3<=n<=100),它表示多边形的边数(当然也是顶点数),然后是按照逆时针顺序给出的n个顶点的坐标(x1, y1, x2, y2... xn, yn),为了简化问题,这里的所有坐标都用整数表示。
Output 对于每个测试实例,请输出对应的多边形面积,结果精确到小数点后一位小数。
Sample Input 3 0 0 1 0 0 1 4 1 0 0 1 -1 0 0 -1 0
Sample Output 0.5 2.0 |
题目大意:中文题。求多边形的面积;
把多边形分成n-2个三角形,求三角形的面积,再求和,对于三角形的面积,用向量叉积
ac:
#include<stdio.h>
#include<string.h>
#include<math.h>
//#include<map>
//#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000007
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b)
#define clean(a,b) memset(a,b,sizeof(a))// 水印
//std::ios::sync_with_stdio(false);
struct node{
double x,y;
// node friend operator - (node a,node b){
// return {a.x-b.x,a.y-b.y};
// }
node& operator - (node &a){
x=x-a.x;
y=y-a.y;
return *this;
}
}dot[110];
double cross(node a,node b)
{
return a.x*b.y-a.y*b.x;
}
int main()//改革风
{
std::ios::sync_with_stdio(false);
int n;
while(cin>>n&&n)
{
for(int i=1;i<=n;++i)
cin>>dot[i].x>>dot[i].y;
if(n<3)
{
cout<<0<<endl;
continue;
}
dot[n+1]=dot[1];
double sum=0;
for(int i=1;i<=n;++i)
sum=sum+cross(dot[i],dot[i+1]);
sum=fabs(sum/2.0);
printf("%.1lf\n",sum);
}
}