Hangzhou Electric Oj brush title (2036)

Reform spring breeze Montreal

Subject description:

"Spring breeze of reform over the floor,
not AC does not matter;
it is not to return home,
as well as third of an acre.
Thank you (band play music)!"

Saying some students an excellent state of mind, you know the game every day, the exam so simple subject, It is 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 reverse clockwise in the order given the n coordinates of the vertices (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.
The output of each instance per line.

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

analysis:

Use Polygon formula : 0.5 * | x1 * y2- y1 * x2 + x2 * y3-y2 * x3 + ...... + xn * y1-yn * x1 |
 

By the answer:

#include<stdio.h>
int main(){
	int n,x[3],y[3]; 
	double sum;
	while(scanf("%d",&n)!=EOF){
		if(n==0)break;
		if(n>=3&&n<=100){
			sum=0.0;
			scanf("%d%d",&x[0],&y[0]);
			x[2]=x[0];                           //存放最后要乘的点 
		    y[2]=y[0];
		    while(--n){
		    	scanf("%d%d",&x[1],&y[1]);
		    	sum+=x[0]*y[1]-x[1]*y[0];
		    	x[0]=x[1];
		    	y[0]=y[1];
			}
			sum+=x[0]*y[2]-y[0]*x[2]; 
		}
		printf("%.1f\n",sum/2);                //和要除以2 
	}
	return 0;
}

 

发布了55 篇原创文章 · 获赞 0 · 访问量 1003

Guess you like

Origin blog.csdn.net/ZhangShaoYan111/article/details/104201450