poj3737(三分搜索)

来源:互联网 发布:无线传感器网络的特点 编辑:程序博客网 时间:2024/05/11 22:24

题意:给出一个圆锥的表面积(侧面积+底面积),求圆锥的最大体积。


解法:三分半径。左边界随便取个极小的数,右边界可以假定这个圆锥是平的,高是0.这是底面积的二倍是表面积。


代码:

/******************************************************* author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>//freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8const double pi=acos(-1.0);typedef long long LL;const int Max=10100;const int INF=1000000007;double v;double vol(double r){    double tool=v-pi*r*r;    double len=tool/pi/r;    double high=sqrt(len*len-r*r);    return pi*r*r*high/3.0;}double gethigh(double r){    double tool=v-pi*r*r;      double len=tool/pi/r;    return sqrt(len*len-r*r);}int main(){    while(scanf("%lf",&v)==1)    {        double left=0.1,right=sqrt(v/2.0/pi);        while(right-left>eps)        {            double middle=(left+right)/2;            double middleright=(middle+right)/2;            if(vol(middle)>vol(middleright))                right=middleright;            else                left=middle;        }        double high=gethigh(left);        printf("%.2f\n",left*left*pi*high/3.0);        printf("%.2f\n",high);        printf("%.2f\n",left);    }    return 0;}

0 0