码迷,mamicode.com
首页 > 其他好文 > 详细

poj3737(三分搜索)

时间:2014-05-15 04:58:12      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   int   

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


解法:三分半径。左边界随便取个极小的数,右边界可以假定这个圆锥是平的,高是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-8
const 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;
}

poj3737(三分搜索),布布扣,bubuko.com

poj3737(三分搜索)

标签:style   blog   class   code   c   int   

原文地址:http://blog.csdn.net/xiefubao/article/details/25694755

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!