POJ 3737

第一道三分题,有模板

#define eps 10e-6
double cal(){}//计算题目所需要的值
while(l+eps<r)
{
    m1=l+(r-l)/3;
    m2=r-(r-l)/3;
    v1=cal(m1);
    v2=cal(m2);
    if(v1<v2)l=m1;
    else r=m2;
}

  

感觉三分像是数学题,因为它求的是单峰极值的问题。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

const double PI=3.141592653;
const double eps=1e-8;

double S,RR,LL,m,mm;

double cal(double r){
	return sqrt(((r*S*S)-(2*PI*S*r*r))/9.0);
}

int main(){
	while(scanf("%lf",&S)!=EOF){
		RR=S/PI;LL=0;
		while(LL+eps<RR){
			m=LL+(RR-LL)/3;
			mm=RR-(RR-LL)/3;
			if(cal(m)<cal(mm))
			LL=m;
			else RR=mm;
		}
		double v=cal(LL);
		printf("%.2lf
%.2lf
%.2lf
",v,v*3/(PI*LL),sqrt(LL));
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/jie-dcai/p/4273316.html