做此题完全是为了练积分。
【普通求导版】
Select Code
#include<cstdio>
#include<cmath>
using namespace std;
typedef double real;
const real eps=1e-5;
int cas,Cas,D,H,B,L;
real F(real a,real x){
real a2=a*a;
real x2=x*x;
return (x*sqrt(a2+x2)+a2*log(fabs(x+sqrt(a2+x2))))/2.0;
}
real calc(real w,real h){
real a=4.0*h/(w*w);
real A=1.0/(2.0*a);
return (F(A,w/2.0)-F(A,0))*4.0*a;
}
int main(){
for(scanf("%d",&cas);cas--;){
scanf("%d%d%d%d",&D,&H,&B,&L);
int n=(B+D-1)/D;
real D1=(real)B/n;
real L1=(real)L/n;
real l=0.0,r=H,mid;
while(l+eps<r){
mid=(l+r)/2.0;
if(calc(D1,mid)<L1) l=mid;else r=mid;
}
printf("Case %d:\n%.2lf\n",++Cas,H-l);
if(cas) putchar(‘\n‘);
}
return 0;
}
【辛普森自适应版】
Select Code
#include<cstdio>
#include<cmath>
using namespace std;
typedef double real;
const real eps=1e-5;
int cas,Cas,D,H,B,L;
real a;
real F(real x){
real a2=a*a;
real x2=x*x;
return sqrt(1.0+4.0*a2*x2);
}
real simpson(real a,real b){
real c=(a+b)/2.0;
return (F(a)+4.0*F(c)+F(b))*(b-a)/6.0;
}
real asr(real a,real b,real eps,real A){
real c=(a+b)/2.0;
real L=simpson(a,c),R=simpson(c,b);
if(fabs(L+R-A)<=15*eps) return L+R+(L+R-A)/15;
return asr(a,c,eps/2,L)+asr(c,b,eps/2,R);
}
real asr(real a,real b,real eps){
return asr(a,b,eps,simpson(a,b));
}
real calc(real w,real h){
a=4.0*h/(w*w);
return asr(0,w/2.0,eps)*2.0;
}
int main(){
for(scanf("%d",&cas);cas--;){
scanf("%d%d%d%d",&D,&H,&B,&L);
int n=(B+D-1)/D;
real D1=(real)B/n;
real L1=(real)L/n;
real l=0.0,r=H,mid;
while(l+eps<r){
mid=(l+r)/2.0;
if(calc(D1,mid)<L1) l=mid;else r=mid;
}
printf("Case %d:\n%.2lf\n",++Cas,H-l);
if(cas) putchar(‘\n‘);
}
return 0;
}
时间: 2024-10-12 11:37:57