33,008
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef struct
{
int x;
int y;
}point;
point result[50001];
point m[50001];
int n,top;
double dis(point a, point b)
{
double distance;
distance = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
distance = sqrt(distance);
return distance;
}
double Multiply(point a, point b, point c)
{
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);//计算叉积
}
int cmp(const void *a, const void *b)
{
point *c, *d;
double t;
c = (point *)a;
d = (point *)b;
t = Multiply(m[0],*c,*d);
if(t < 0)
return 1;
else if(t == 0 && (dis(m[0],*c) < dis(m[0],*d)))
return 1;
else
return -1;
}
void tubao()
{
int i;
result[0] = m[0];
result[1] = m[1];
result[2] = m[2];
top = 2;
for(i = 3; i <= n; i ++)
{
while(Multiply(result[top - 1],result[top], m[i]) <= 0 && top >= 2)//叉积为负 向右拐 即凹;
{ //叉积为正 向左拐 即凸.
top --;
}
result[++ top] = m[i];
}
}
int main()
{
int i,j,k,px,py,p;
point temp;
double a,b,c,s;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d%d",&m[i].x,&m[i].y);
if(i == 0 || m[i].y < py)
{
px = m[i].x;
py = m[i].y;
p = i;
}
else if(m[i].y == py && m[i].x < px)
{
px = m[i].x;
py = m[i].y;
p = i;
}
}
temp=m[0];m[0]=m[p];m[p]=temp;
qsort(m+1,n-1,sizeof(m[0]),cmp);
m[n]=m[0];
tubao();
double area[50001];
double max =0;
for(i=0;i<top;i++) //求面积
{
for(j=i+1;j<top;j++)
{
for(k=j+1;k<top;k++)
{
a = dis(result[i],result[j]);//海伦公式
b = dis(result[i],result[k]);
c = dis(result[j],result[k]);
s=(a+b+c)/2;
a=s-a;
b=s-b;
c=s-c;
area[i]=s*a*b*c;
area[i]=sqrt(area[i]);
if(max<area[i])
max=area[i];
}
}
}
printf("%.2lf\n",max);
}
return 0;
}
#include<iostream>
#include<cmath>
#define SIZE 50010
using namespace std;
int N;
typedef struct
{
double x;
double y;
}Point;
Point p[SIZE], stack[SIZE];
int top;
double Mul(Point p1, Point p2, Point p3)
{
return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
}
double Dis(Point a, Point b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int cmp(const void *a, const void *b)
{
Point c = *(Point *)a;
Point d = *(Point *)b;
double k = Mul(p[0], c, d);
if(k < 0 || (!k && Dis(c, p[0]) > Dis(d, p[0])))
return 1;
return -1;
}
void Convex()
{
for(int i = 1; i < n; i++)
{
Point temp;
if(p[i].y < p[0].y || (p[i].y == p[0].y && p[i].x < p[0].x))
{
temp = p[i];
p[i] = p[0];
p[0] = temp;
}
}
qsort(p + 1, n - 1, sizeof(p[0]), cmp);
stack[0] = p[0];
stack[1] = p[1];
stack[2] = p[2];
top =2;
for(int i = 3; i < n; i++)
{
while(top >= 2 && Mul(stack[top - 1], stack[top], p[i]) <= 0)
top--;
top++;
stack[top] = p[i];
}
}
int main()
{
int i, j, k;
while(cin >> N, N){
for(i = 0; i < N; i++)
cin >> p[i].x >> p[i].y;
Convex();
double max_area = 0;
double m;
for(i = 0; i <= top - 2 ; i++)
{
for(j = i + 1; j <= top - 1; j++)
for(k = j + 1; k <= top; k++)
{
m = fabs(Mul(stack[i], stack[j], stack[k]) / 2);
max_area = m > max_area ? m : max_area;
}
}
cout.setf(ios::fixed);
cout.precision(2);
cout << max_area << endl;
}
return 0;
}