自适应辛普森法模板题 UVAlive3485;
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<stack>
#define ll long long
#define MAX 1000
#define INF INT_MAX
#define EPS 1e-5
using namespace std;
double a;
double f(double x){ //被积函数
return sqrt(1 + 4*a*a*x*x);
}
double simpson(double x, double y){ //三点辛普森公式
double z = x + (y - x) / 2;
return (f(x) + 4*f(z) + f(y))*(y-x) / 6;
}
double asr(double x, double y, double eps, double A){ //自适应辛普森法,求函数f在区间[x,y]上的积分
double z = x + (y-x) / 2;
double L = simpson(x,z), R = simpson(z,y);
if (fabs(L+R-A) <= 15*eps) return L + R + (L+R-A) / 15.0;
return asr(x,z,eps/2,L) + asr(z,y,eps/2,R);
}
double Length(double w, double h){
a = 4.0*h/(w*w);
return solve(0,w/2,1e-5,simpson(0,w/2)) * 2;
}
int main(){
int T,D,H,B,L;
scanf("%d",&T);
for (int cas = 1; cas <= T; cas++){
scanf("%d%d%d%d",&D,&H,&B,&L);
int n = (B+D-1) / D; //注意向上取整
double w = (double)B / n;
double l = (double)L / n;
double x = 0.0, y = H*1.0;
while (y - x > EPS){
double m = x + (y-x) / 2.0;
if (Length(w,m) > l){
y = m;
}
else x = m;
}
printf("Case %d:\n%.2lf\n",cas,H - x);
if (cas < T) printf("\n");
}
return 0;
}