您的位置:首页 > 其它

YT14-HDU-求抛物线与直线间的面积(积分)

2015-02-05 21:49 323 查看


Problem 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



Hint

For float may be not accurate enough, please use double instead of float.

代码如下:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int T;
    double x1,y1,x2,y2,x3,y3,a,k,s;
    cin>>T;
    while(T--)
    {
        cin>>x1>>y1>>x2>>y2>>x3>>y3;
        a=(y2-y1)/((x2-x1)*(x2-x1));
        k=(y2-y3)/(x2-x3);
        s=a/3*(x3*x3*x3-x2*x2*x2)+(a*x1*x1+k*x3+y1-y3)*(x3-x2)+0.5*(2*a*x1+k)*(x2*x2-x3*x3);
        cout<<setiosflags(ios::fixed)<<setprecision(2)<<s<<endl;
    }
    return 0;
}


运行结果图片由于当时做题后没有保存,就不再上传了。

这道题考得是求抛物线方程y=ax^2+bx+c中啊a,b,c的值,然后就是微积分,提交的这个代码是在百度上找的,但没有分析,所以我也没怎么看懂,反而是另一位的代码差不多理解了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
   double x1,x2,x3,y1,y2,y3,a,b,c,p,q,r,k,d,ans;
   int T;
   scanf("%d",&T);
   while(T--)
   {
       scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
       p=y1/((x1-x2)*(x1-x3));
       q=y2/((x2-x1)*(x2-x3));
       r=y3/((x3-x1)*(x3-x2));
       a=p+q+r;
       b=-p*(x2+x3)-q*(x1+x3)-r*(x1+x2);
       c=p*x2*x3+q*x1*x3+r*x1*x2;
       k=(y3-y2)/(x3-x2);//直线的斜率
       d=y2-k*x2;//直线的截距
       ans=1.0/3.0*a*(x3*x3*x3-x2*x2*x2)+0.5*(b-k)*(x3*x3-x2*x2)+(c-d)*(x3-x2);
       printf("%.2lf\n",ans);
   }
    return 0;
}


设已知3点为(x1,y1),(x2,y2),(x3,y3) 方程设为 y=a*x^2+b*x+c;

令       p=y1/((x1-x2)*(x1-x3));
       q=y2/((x2-x1)*(x2-x3));
       r=y3/((x3-x1)*(x3-x2));

则      a=p+q+r;
       b=-p*(x2+x3)-q*(x1+x3)-r*(x1+x2);
       c=p*x2*x3+q*x1*x3+r*x1*x2;

然后就是微分了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: