首页 > 代码库 > hdu 4709

hdu 4709

求三角形的面积

利用向量点乘得到三角形的面积

三个for循环嵌套即可


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

const double INF = 1e9+50;

double x[1000];
double y[1000];

double area(int i, int j, int k)
{
	double ax = x[i] - x[j];
	double ay = y[i] - y[j];
	double bx = x[k] - x[j];
	double by = y[k] - y[j];
	
	double ans = (ax*by - ay*bx)/2;
	if(ans < 0) return -ans;
	return ans;
}

int main()
{
	int T, N;
	double a, b, Min;
	scanf("%d", &T);
	while(T --)
	{
		Min = INF;
		scanf("%d", &N);
		for(int i = 0; i < N; i ++)
		{
			scanf("%lf%lf", &a, &b);
			x[i] = a;
			y[i] = b;
		}
		for(int i = 0; i < N; i ++)
			for(int j = i+1; j < N; j ++)
				for(int k = j+1; k < N; k ++)
				{
					double s = area(i, j, k);
					if(s < Min && s != 0.0) Min = s; 
				}		
		if(Min != INF){
			printf("%.2lf\n", Min);
		}
		else
		{
			printf("Impossible\n");
		}
	}
	
	return 0;
}