51Nod

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
", ans );
	}
	return 0; 
}

  

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