二分 poj1905 Expanding Rods

题意:一根细杆受热中间会凸起,问突起的高度


思路:二分角度

把方程列出来,然后二分就可以了,最后再通过角度求那个凸起的高度就行

但是一定要注意精度问题,要么写成R-L>=1e-14,因为是二分角度,所以精度一定要设的很高


但是通过精度去判断循环的终点,并不是一个特别好的习惯,要改

改成直接二分100次,这样就完美解决了任何题目的精度问题!


#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int MX = 17;
const int INF = 0x3f3f3f3f;
const double exps = 1e-14;
const double pi = acos(-1.0);

double d, n, C, len;

double f(double p) {
    return p * d / sin(p) - len;
}

int main() {
    while(~scanf("%lf%lf%lf", &d, &n, &C), d >= 0) {
        len = (1 + n * C) * d;
        double L = 0, R = pi, m;
        for(int i = 1; i < 100; i++) {
            m = (L + R) / 2;
            if(f(m) < 0) {
                L = m;
            } else {
                R = m;
            }
        }

        if(L < exps) {
            printf("0.000\n");
        } else {
            printf("%.3lf\n", (1 - cos(L))*d / (2 * sin(L)));
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值