题意:给出n个点,求能够成的凸四边形的个数,题目已给出任意三个点不会在一条线上。
链接:http://acm.fzu.edu.cn/problem.php?pid=2148
思路:凹四边形任意三个点构成的三边形的面积一定有一个等于其余三个之和。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
struct node
{
double x,y;
}a[40];
double compute(node s, node d, node f) //注意返回double型
{
return fabs((s.x * d.y + d.x * f.y + f.x * s.y - s.x * f.y - d.x * s.y - f.x * d.y) / 2);
}
int main()
{
int t,cnt=0;
cin >> t;
while(t--)
{
int n;
cin >> n;
for(int i=0; i<n;i++)
cin >> a[i].x >> a[i].y;
int sum=0;
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
for(int k=j+1; k<n; k++)
{
for(int l=k+1; l<n; l++)
{
double e = compute(a[i],a[j],a[k]);
double b = compute(a[l],a[j],a[k]);
double c = compute(a[i],a[l],a[k]);
double d = compute(a[i],a[j],a[l]);
if(fabs(e-b-c-d)<1e-9 ||fabs(b-e-c-d)<1e-9 ||fabs(c-e-b-d)<1e-9 ||fabs(d-e-b-c)<1e-9)
{
continue;
}
sum++;
}
}
}
}
cout << "Case "<< ++cnt << ": " << sum << endl;
}
return 0;
}