问题及代码:
运行结果:
求不定积分。
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:area.cpp
*作 者:单昕昕
*完成日期:2015年2月3日
*版 本 号:v1.0
*
*问题描述: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.
*程序输入: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).
*程序输出: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.
*/
//我写的代码,测试结果是对的,但是WA了,不知道问题出在哪。
#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
#define N 1000
int main()
{
int t;
double x1,x2,x3,y1,y2,y3,s,y,h,a;
cin>>t;
while(t--)
{
cin>>x1>>y1>>x2>>y2>>x3>>y3;
int i;
a=(y2-y1)/((x2-x1)*(x2-x1));
s=(a*(x2-x1)*(x2-x1)+y1-(y2+((x2-x2)*(y3-y2))/(x3-x2))+a*(x3-x1)*(x3-x1)+y1-(y2+((x3-x2)*(y3-y2))/(x3-x2)))/2.0;
h=(x3-x2)/N;
for(i=1; i<N; i++)
{
y=x2+i*h;
s+=(a*(y-x1)*(y-x1)+y1-(y2+((y-x2)*(y3-y2))/(x3-x2)));
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<s*h<<endl;
}
return 0;
}
//AC代码,网上找的。
#include <stdio.h>
#include <math.h>
const double e=1e-3; //控制精度
double a,h,d;
double fx(double x)
{
return a*(x-h)*(x-h)+d;
}
double area(double x1,double y1,double x2,double y2,double x3,double y3)
{
double d1,d2,d3,p,s,x4,y4;
d1=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
if (d1<e) return 0; //精度有限
d2=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
d3=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); //求出3条边长
p=(d1+d2+d3)/2;
s=sqrt(p*(p-d1)*(p-d2)*(p-d3)); //海伦公式求三角形面积
x4=(x1+x2)/2; y4=fx(x4);
s+=area(x4,y4,x1,y1,x2,y2);
x4=(x1+x3)/2; y4=fx(x4);
s+=area(x4,y4,x1,y1,x3,y3); //二分法 求余下的面积
return s;
}
int main()
{
double x1,y1,x2,y2,x3,y3,s;
int n,i;
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
h=x1; d=y1;
a=(y2-d)/(x2-h)/(x2-h); //求二次函数解析式
s=area(x1,y1,x2,y2,x3,y3);
printf("%.2lf\n",s);
}
}
运行结果:
求不定积分。
学习心得:
我是用高数学的面积求法写出的代码,求大神看看为什么WA了。。
AC代码是将所围成的图形分割成一个三角形和两个弓形来逼近求得。