计算几何+数值计算的题目;
要用到辛普森积分,没有学过~~~
参考学习了 acm_Naruto大神 的代码!
代码:
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #define maxn 20005 5 using namespace std; 6 int n,a,b,c,l,r,x[maxn],y[maxn]; 7 double sqr(double x) 8 { 9 return x*x; 10 } 11 double len(double x) 12 { 13 double t,tt; 14 t=(sqrt((b+2*a*x)*(b+2*a*x)+1)+2*a*x+b); 15 tt=log(t)/(4*a); 16 t=sqrt((b+2*a*x)*(b+2*a*x)+1)*(b+2*a*x)/(4*a); 17 tt+=t; 18 return tt; 19 } 20 21 int main() 22 { 23 while(scanf("%d%d%d%d%d%d",&n,&a,&b,&c,&l,&r)!=EOF) 24 { 25 for(int i=0; i<n; i++) 26 scanf("%d%d",&x[i],&y[i]); 27 x[n]=x[0],y[n]=y[0]; 28 double ans=0; 29 for(int i=0; i<n; i++)if(x[i]!=x[i+1]) 30 { 31 double x0=x[i],y0=y[i]; 32 double x1=x[i+1],y1=y[i+1]; 33 if(x0>x1)swap(x0,x1),swap(y0,y1); 34 double k=(y1-y0)/(x1-x0); 35 double d=y1-k*x1; 36 double u=sqr(b-k)-4*a*(c-d);//b*b-4*a*c; 37 double length=0; 38 if(u>=0) 39 { 40 double t1=(-(b-k)*1.0+sqrt(u))/(2.0*a);//方程的根; 41 double t2=(-(b-k)*1.0-sqrt(u))/(2.0*a);//方程的根; 42 if(t1>t2)swap(t1,t2); 43 t1=max(x0,max(t1,l*1.0)); 44 t2=min(x1,min(t2,r*1.0)); 45 if(t1<=t2) 46 length=fabs(len(t2)-len(t1));//辛普森积分; 47 if(x[i]<x[i+1])ans+=length; 48 else ans-=length; 49 } 50 } 51 printf("%.2lf ",fabs(ans)); 52 } 53 return 0; 54 }