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

51Nod - 1629 B君的圆锥

时间:2017-05-14 12:33:08      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:表示   space   text   51nod   ref   第一个   最大   log   三分   

51Nod - 1629 B君的圆锥

B君要用一个表面积为S的圆锥将白山云包起来。
 
B君希望包住的白山云体积尽量大,B君想知道体积最大可以是多少。
 
注意圆锥的表面积包括底面和侧面。
Input
一行一个整数,表示表面积S。(1 <= S <= 10^9)
Output
一行一个实数,表示体积。
Input示例
8
Output示例
1.504506

 

 

题解: 

    想使用三分法来解决,但是在9个test中只通过了第一个。(不知道出错在哪里了) 

    后转 直接公式法解决。 

 

 

#include <iostream> 
#include <cstdio> 
#include <cmath> 
using namespace std; 
const double PI = 3.14159265358979; 

inline double fun(double x, const int &n){
	return PI*x*x*sqrt(n*n/(PI*PI*x*x) - 2*n/PI)/3;   
}

double solve(double l, double r, const int &n){
	double tmp_l, tmp_r; 
	while( (r-l) >= 1e-8 ){
		tmp_l = (r-l)/3.0 + l; 
		tmp_r = (r-l)*2.0/3.0 + l; 
		if( fun(tmp_r, n) > fun(tmp_l, n) ){
			l = tmp_l; 
		}else{
			r = tmp_r; 
		}
	}
	return fun(l + (r-l)/2.0, n); 
}

int main(){

	int n; 
	double l, r, ans; 
	while(scanf("%d", &n) != EOF){
// 		l = 0.0000001; 
// 		r = sqrt(n*1.0 / PI); 
// 		ans = solve(l, r, n); 
		ans = n*1.0*sqrt(n*1.0/(72.0*PI)); 
		printf("%.7f\n", ans );
	}
	return 0; 
}

  

 

51Nod - 1629 B君的圆锥

标签:表示   space   text   51nod   ref   第一个   最大   log   三分   

原文地址:http://www.cnblogs.com/zhang-yd/p/6851817.html

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