HDOJ 2036 改革春风吹满地

用到了数学的知识:

求多边形面积:(可以是凸多边形或者凹多边形)

S = 1/2 * abs(∑(xiyi+1 – xi+1yi))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
using namespace std;
typedef struct point{
 int x, y;
}point;
 
point polygon[101];
int main()
{
 int num;
 while(scanf("%d", &num) && num)
 {
  double area = 0.0;
  for(int i = 0; i < num; ++i)
   scanf("%d %d", &polygon[i].x, &polygon[i].y);
  for(int i = 0; i < num; ++i)
   area += (polygon[i].x * polygon[(i+1)%num].y) - (polygon[(i+1)%num].x * polygon[i].y);
  printf("%.1lf\n", area/2.0);
 }
 return 0;
}

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]

发表评论

电子邮件地址不会被公开。 必填项已用*标注