HDU_ reform spring breeze Montreal

Introduction: feel the use of mathematics really is not flexible ~

Subject description:
"spring breeze of reform over the floor,

AC does not matter;

It is not back home,

There are third acre of land.

Thank you! (Band play music). "

Some students saying excellent state of mind, you know the game every day, the exam so simple subject, but also foggy, but also turned out to be so few limericks.

Okay, the teacher's responsibility is to help you solve the problem, since it would like to farm, it points you one.

This field is located in Cangnan County of Wenzhou City, Zhejiang Province Lingxi Linjiapuzi village, polygon-shaped piece of land, originally linle now ready to give you. However, everything is not so simple, you must first tell me how much of the land area in the end, if answered correctly in order to really get the land.

Worry about, right? Is to let you know, farming is also a need AC knowledge! After it was good practice ...

Input
input data comprising a plurality of test cases, one row for each test case, the beginning of each line is an integer n (3 <= n <= 100), which represents the number of polygon (of course, the number of vertices) edge, then in accordance with n counterclockwise order given vertex coordinates (x1, y1, x2, y2 ... xn, yn), in order to simplify the problem, where all coordinates are represented by integer.

The input data are all integers in the range of 32-bit integers, n = 0 indicates the end of the data without processing.

Output
For each test case, output a polygon area corresponding to the result of a decimal decimal place.

Examples of the output of each row for
the Sample the Input
. 3. 1 0 0 0 0. 1
. 4. 1. 1 0 -1 0 0 0 -1
0

The Output the Sample
0.5
2.0
solving key:
understood that the method of vector cross product seeking area, the seeking of adjacent cross-product, plus up.
Arbitrary polygon area formula:

= 0.5 * S (X1 Y2-X2 Y1 + X2 Y3 X3- Y2 + ... XK Y1 YK- X1);
this can not be calculated using Heron's formula, it can not calculate the shape of the concave edge area.

#include<stdio.h>
#include<math.h>
int main()
{
	 int n;
	 int x[100],y[100];
	 while(scanf("%d",&n) && n != 0)
	 {
		  double t = 0.0;
		  
		  for(int i = 0; i < n; i++)
		  {
		   scanf("%d %d",&x[i],&y[i]); 
		  }
		  for(int i = 0; i < n - 1 ;i++)
		  {
		   t = t + ((double)fabs((x[i] * y[i+1] - x[i+1] * y [i]))/2);
		  }
		   t = t + ((double)fabs(x[n-1] * y[0] - y[n-1] * x[0]))/2;
		  
		  printf("%.1lf\n",t);
	 }
}
Published 34 original articles · won praise 85 · views 4626

Guess you like

Origin blog.csdn.net/weixin_45895026/article/details/103861606