题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010
题目大意:给你n个点,问你顺次连线能否连成多边形?如果能,就输出多边形面积。
面积用向量的叉积去算。然后能否连成多边形就是看这条线跟之前的线有没有交点。
这些在大白书上都有板子。。
代码:
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <functional>
#include <cmath>
#include <numeric>
#include <ctime>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define PB push_back
#define MP make_pair
#define SZ size()
#define CL clear()
#define AA first
#define BB second
#define EPS 1e-8
#define ZERO(x) memset((x),0,sizeof(x))
const int INF = ~0U>>;
const double PI = acos(-1.0); struct POINT{
double x,y;
POINT(double ax=,double ay=):x(ax),y(ay) {}
};
typedef POINT VECTOR; POINT pts[]; VECTOR operator- (POINT A,POINT B){
return VECTOR(A.x-B.x,A.y-B.y);
} double CROSS(VECTOR A,VECTOR B){
return A.x*B.y-A.y*B.x;
} double AREA(POINT *p,int n){
double area = ;
for(int i=;i<n-;i++){
area += CROSS(p[i]-p[],p[i+]-p[]);
}
return area/;
} bool SegmentProperIntersection(POINT a1,POINT a2,POINT b1,POINT b2){
double c1 = CROSS(a2-a1,b1-a1) , c2 = CROSS(a2-a1,b2-a1),
c3 = CROSS(b2-b1,a1-b1) , c4 = CROSS(b2-b1,a2-b1);
return c1*c2<EPS && c3*c4<EPS;
} int main(){
int n;
int kase = ;
while( scanf("%d",&n), n ){
if(kase!=) puts("");
bool flag = true;
for(int i=;i<n;i++){
double a,b;
scanf("%lf%lf",&a,&b);
pts[i] = POINT(a,b);
for(int j=;j<i-;j++){
if( SegmentProperIntersection(pts[j],pts[j+],pts[i-],pts[i])) flag = false;
}
}
for(int j=;j<n-;j++){
if( SegmentProperIntersection(pts[j],pts[j+],pts[],pts[n-])) flag = false;
}
double area = fabs(AREA(pts,n));
if( n==||n== ){
printf("Figure %d: Impossible\n",kase++);
continue;
}
if(!flag) printf("Figure %d: Impossible\n",kase++);
else printf("Figure %d: %.2f\n",kase++,area);
}
return ;
}