HDU 1071 解方程

Description

Ignatius bought a land last week, but he didn't know the area of the land because the land is enclosed by a parabola and a straight line. The picture below shows the area. Now given all the intersectant points shows in the picture, can you tell Ignatius the area of the land? 

Note: The point P1 in the picture is the vertex of the parabola. 


 

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. 
Each test case contains three intersectant points which shows in the picture, they are given in the order of P1, P2, P3. Each point is described by two floating-point numbers X and Y(0.0<=X,Y<=1000.0). 
 

Output

For each test case, you should output the area of the land, the result should be rounded to 2 decimal places. 
 

Sample Input

2 5.000000 5.000000 0.000000 0.000000 10.000000 0.000000 10.000000 10.000000 1.000000 1.000000 14.000000 8.222222
 

Sample Output

33.33 40.69

題意:給你三個點的座標,求面積。

思路:額,沒想到好的,強行解三元一次方程組把a,b,c解出來,再利用積分求解。。


這個更簡單,拋物線頂點座標表示公式:y=a(x-x1)^2+y1,代入x2或x3座標可解得拋物線公式,再利用x2,x3,解y=kx+b,積分就可以了。

#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
using namespace std;

void swap(double *a,double *b){ //遇到除以零的情況時,兩行交換位置 
	double t;
	t=*a;
	*a=*b;
	*b=t;
}
int main(){
	freopen("input.txt","r",stdin);
	int t;
	cin>>t;
	while(t--){
		double a,b,c,u,v,uy,vy;
		double x1,y1,x2,y2,x3,y3,x1_2,x2_2,x3_2,m1=1,m2=1,m3=1;
		cin>>x1>>y1>>x2>>y2>>x3>>y3;
		u=x2;uy=y2;
		v=x3;vy=y3;
		
		x1_2=x1*x1;
		x2_2=x2*x2;
		x3_2=x3*x3;
		
		if(x1_2==0){
			swap(&x1_2,&x2_2);
			swap(&x1,&x2);
			swap(&m1,&m2);
			swap(&y1,&y2);
		}
		x2=x2-x2_2/x1_2*x1;
		m2=m2-x2_2/x1_2*m1;
		y2=y2-x2_2/x1_2*y1;
 //       printf("x2=%.2f\nm2=%.2f\ny2=%.2f\n",x2,m2,y2);
        
		if(x1_2==0){
			swap(&x1_2,&x2_2);
			swap(&x3,&x2);
			swap(&m3,&m2);
			swap(&y3,&y2);
		}		
		x3=x3-x3_2/x1_2*x1;
		m3=m3-x3_2/x1_2*m1;
		y3=y3-x3_2/x1_2*y1;
//		printf("x3=%.2f\nm3=%.2f\ny3=%.2f\n",x3,m3,y3);

		if(x2==0){
			swap(&x3,&x2);
			swap(&m3,&m2);
			swap(&y3,&y2);
		}
		m3=m3-x3/x2*m2;
		y3=y3-x3/x2*y2;
//		printf("m3=%.2f\ny3=%.2f\n",m3,y3);
		
		c=y3/m3;
		b=(y2-m2*c)/x2;
		a=(y1-m1*c-x1*b)/x1_2;
		
//		printf("a=%.2f\nb=%.2f\nc=%.2f\n",a,b,c);
		
		double S,S1,S2;
		S1=(a/3*v*v*v+b/2*v*v+c*v)-(a/3*u*u*u+b/2*u*u+c*u);
//		printf("S1=%.2f\n",S1);
		S2=(uy+vy)*(v-u)/2.0;
//		printf("S2=%.2f\n",S2);
		printf("%.2f\n",S1-S2);
	}
	return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章