poj3737

三分法求单峰函数极值

ContractedBlock.gif ExpandedBlockStart.gif View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
using namespace std;

#define pi acos(double(-1))
#define eps 10E-11

double s;

int dblcmp(double &a, double &b)
{
if (a - eps > b)
return 1;
if (a + eps < b)
return -1;
return 0;
}

double cal(double &r)
{
double R = s / pi / r - r;
double h = sqrt(R * R - r * r);
return r * r * pi * h / 3;
}

int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%lf", &s) != EOF)
{
double l = 0;
double r = sqrt(s / pi);
while (dblcmp(l, r) < 0)
{
double r1 = l + (r - l) / 3;
double r2 = r - (r - l) / 3;
double vol1 = cal(r1);
double vol2 = cal(r2);
if (dblcmp(vol1, vol2) < 0)
l
= r1;
else
r
= r2;
}
double R = s / pi / r - r;
double h = sqrt(R * R - r * r);
double vol = r * r * pi * h / 3;
printf(
"%.2f\n%.2f\n%.2f\n", vol, h, r);
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值