题目:http://acm.hit.edu.cn/hoj/problem/view?id=2485
求给定点组成的三角形的面积最大值。并且三角形内部不能有其他点。
暴力+叉积即可。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;
struct Point
{
char c;
int x;
int y;
};
Point p[20];
//求面积绝对值
double cross(Point O,Point A,Point B)
{
return fabs((A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y))/2;
}
//o是否在ABC中,在返回true
bool judge(Point A,Point B,Point C,Point O)
{
double area = cross(A,B,C);
double s = 0;
s += cross(O,A,B);
s += cross(O,B,C);
s += cross(O,C,A);
if(fabs(area-s)<=1e-8)
{
return true;
}
else
{
return false;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n;
while(scanf(" %d",&n)!=EOF && n!=0)
{
double area = 0;
Point maxA,maxB,maxC;
for(int i=1; i<=n; i++)
{
scanf(" %c %d %d",&p[i].c,&p[i].x,&p[i].y);
}
for(int i=1; i<=n; i++)
{
for(int j=i+1; j<=n; j++)
{
for(int k=j+1; k<=n; k++)
{
int flag = 0;
for(int t=1; t<=n; t++)
{
if(t == i || t == j || t == k)
{
continue;
}
if(judge(p[i],p[j],p[k],p[t]))
{
flag = 1;
break;
}
}
//满足条件的三角形
if(flag == 0)
{
double temp = cross(p[i],p[j],p[k]);
if(temp > area)
{
area = temp;
maxA = p[i];
maxB = p[j];
maxC = p[k];
}
}
}
}
}
printf("%c%c%c\n",maxA.c,maxB.c,maxC.c);
}
return 0;
}