【题目大意】:给出一个圆锥体的表面积,求最大的体积,并输出其半径和高。
【解题思路】:下午在比赛的时候是直接求导推的公式做的。
晚上回来想想其实三分极值可以做,但是一直wa,不知道为什么。
我原来三分是这么写的 mid1=(low+high)/2.0; mid2=(mid+high)/2.0; 但是一直过不了,后来改成mid1=low+(high-low)/3.0; mid2=high-(high-low)/3.0;就过了~~不知道什么原因。留着思考....
【代码】:
//三分
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
using namespace std;
#define eps 1e-10
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
double s;
double solve_v(double r){
return pi*r*r*sqrt(pow((s-pi*r*r)/(pi*r),2)-r*r)/3;
}
int main(){
while (~scanf("%lf",&s)){
double v=0.0;
double low=0,high=sqrt(s/pi);
while (low+eps<high){
double mid1=low+(high-low)/3.0;
double mid2=high-(high-low)/3.0;
double v1,v2;
v1=solve_v(mid1);
v2=solve_v(mid2);
if (v1>v2) v=v1,high=mid2;
else v=v2,low=mid1;
}
double h=sqrt(pow((s-pi*high*high)/(pi*high),2)-high*high);
printf("%.2f\n%.2f\n%.2f\n", v,h,high);
}
return 0;
}
//求导
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
using namespace std;
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
double s,r,h,v,l;
void solve(){
r=sqrt(s/(4*pi));
l=s/(pi*r)-r;
h=sqrt(l*l-r*r);
v=1.0/3*pi*r*r*h;
}
int main() {
while (~scanf("%lf",&s)){
solve();
printf ("%.2f\n",v);
printf ("%.2f\n",h);
printf ("%.2f\n",r);
}
return 0;
}