[HDU] [2036] The Spring Breeze of Reform Blows Everywhere

Problem Description

Loading...

Problem Analysis

Find the area of an arbitrary polygon
At that time I hadn’t learned cross products or anything like that yet

A small trick is to use i+1%n
to connect the last point to the first point

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
struct node {
	int x,y;
} a[120];
double f(int x1,int y1,int x2,int y2) {
	return double(x1*y2-x2*y1);
}
int main() {
	int n;
	while(scanf("%d",&n),n) {
		for(int i=0; i<n; i++) {
			scanf("%d %d",&a[i].x,&a[i].y);
		}
		double res=0;
		for(int i=0; i<n; i++) {
			res+=f(a[i].x,a[i].y,a[(i+1)%n].x,a[(i+1)%n].y);
		}
		printf("%.1lf\n",res/2.0);
	}
	return 0;
}

Reference articles: Calculating the area of an arbitrary polygon - Professional Thinker and Practitioner Formula for the area of an arbitrary polygon - Caitou Solving for the area of an arbitrary polygon - songgaoyong - ChinaUnix Blog

Problem link:【HDU】[2036] Reform Spring Breeze Blows Everywhere

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...