51 Nod 1629 B君的圆锥

1629 B君的圆锥 

基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题

 收藏

 关注

B君要用一个表面积为S的圆锥将白山云包起来。

 

B君希望包住的白山云体积尽量大,B君想知道体积最大可以是多少。

 

注意圆锥的表面积包括底面和侧面。

Input

一行一个整数,表示表面积S。(1 <= S <= 10^9)

Output

一行一个实数,表示体积。

Input示例

8

Output示例

1.504506

思路:用三分法求最大值即可。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#define PI (3.1415926535)
//#define PI acos(-1.0)
#define EPS (1e-7)
using namespace std;
double S; //圆锥的表面积
double f(double r) //已知圆锥底面半径r,计算当前圆锥的体积
{     double R=(S-PI*r*r)/PI/r; //侧面展开的扇形半径
      double h=sqrt(R*R-r*r); //
      return PI*r*r*h/3;
}
double three_devide(double low,double up)
{
    double m1,m2;
    while(up-low>=EPS)
    {
        m1=low+(up-low)/3;
        m2=up-(up-low)/3;
        if(f(m1)<=f(m2))
            low=m1;
        else
            up=m2;
    }
    return (m1+m2)/2;
}

int main()
{
#ifndef ONLINE_JUDGE
    //freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    scanf("%lf",&S);
    double ans=f(three_devide(0,S+100));
    printf("%f\n",ans);
    return 0;
}

 

posted @ 2018-10-02 21:05  erge1998  阅读(90)  评论(0编辑  收藏  举报