POJ 1905 Expanding Rods 浮点数二分

Expanding Rods

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11145   Accepted: 2879

Description

When a thin rod of length L is heated n degrees, it expands to a new length L‘=(1+n*C)*L, where C is the coefficient of heat expansion.

When a thin rod is mounted on two solid walls and then heated, it expands and takes the shape of a circular segment, the original rod being the chord of the segment.

Your task is to compute the distance by which the center of the rod is displaced.

Input

The input contains multiple lines. Each line of input contains three non-negative numbers: the initial lenth of the rod in millimeters, the temperature change in degrees and the coefficient of heat expansion of the material. Input data guarantee that no rod
expands by more than one half of its original length. The last line of input contains three negative numbers and it should not be processed.

Output

For each line of input, output one line with the displacement of the center of the rod in millimeters with 3 digits of precision.

Sample Input

1000 100 0.0001
15000 10 0.00006
10 0 0.001
-1 -1 -1

Sample Output

61.329
225.020
0.000

Source

Waterloo local 2004.06.12

题解

又是二分答案的题目。题意是说,一个长为l的细杆会变长,如果固定两个端点,那么这个杆子就会拱起一个圆弧。求圆弧的高度。

如图:

我一开始想的比较简单,直接上手认为是一个可以搞得函数找零点的问题,但是手推函数之后发现一个问题。

对,这个函数是没有极限的,而且在不停地波动中,所以直接去计算零点就容易跪。所以就还是直接模拟去算了。代码挺简单的,主要还是精度问题。

代码示例

/****
	*@author    Shen
	*@title     poj 1905
	*/

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

const double eps = 1e-5;
const double inf = 0xfffffff;
const double PI  = acos(1.0);

double l, n, c, lp;

inline bool test(double x)
{
    double angle = asin(l / x);
    double tmplp = x * angle;
    return tmplp > lp;
}

double Bsearch(double l, double r)
{
    while (r - l > eps)
    {
        double mid = (r + l) * 0.5;
        //printf("%16.4lf%16.4lf%16.4lf\n", l, mid, r);
        if (test(mid)) l = mid;
        else r = mid;
    }
    return r;
}

void solve()
{
    if (n * c < 0.000001)
        printf("0.000\n");
    else
    {
        lp = (1.0 + n * c) * l;
        lp /= 2; l /= 2;//都除二,减少运算
        double r = Bsearch(0.0, inf);
        double h = r - sqrt(r * r - l * l);
        //G++
            printf("%.3f\n", h);
        //C++
        //  printf("%.2lf\n", h);
    }
}

int main()
{
    while (~scanf("%lf%lf%lf", &l, &n, &c))
        if (l < 0) break;
        else solve();
    return 0;
}

POJ 1905 Expanding Rods 浮点数二分

时间: 2024-11-05 18:48:36

POJ 1905 Expanding Rods 浮点数二分的相关文章

poj 1905 Expanding Rods (数学 计算方法 二分)

题目链接 题意:将长度为L的棒子卡在墙壁之间.现在因为某种原因,木棒变长了,因为还在墙壁之间,所以弯成了一个弧度,现在求的是弧的最高处与木棒原先的地方的最大距离. 分析: 下面的分析是网上别人的分析: 设弦长为L0(即原长),弧长为L1=(1+n*C)*l0,目标值为h,半径为R,弧所对圆心角为2θ(弧度制).可以得到以下方程组:圆的弧长公式:L1=2θR三角函数公式:L0=2*R*sinθ,变换得θ=arcsin(L0/(2*R))勾股定理:R^2=(R-h)^2+(0.5*L0)^2,变换得

POJ 1905 Expanding Rods (二分+计算几何+精度处理)

题目地址:POJ 1905 用二分枚举h,然后判断弧长是否符合条件.重点还是在精度问题上,具体看代码吧.. #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #includ

POJ 题目1905 Expanding Rods(二分,数学几何)

Expanding Rods Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12827   Accepted: 3311 Description When a thin rod of length L is heated n degrees, it expands to a new length L'=(1+n*C)*L, where C is the coefficient of heat expansion. Whe

poj 1905 Expanding Rods(木杆的膨胀)【数学计算+二分枚举】

Expanding Rods Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13516   Accepted: 3484 Description When a thin rod of length L is heated n degrees, it expands to a new length L'=(1+n*C)*L, where C is the coefficient of heat expansion. Whe

POJ 1905 Expanding Rods

Expanding Rods Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12796   Accepted: 3299 Description When a thin rod of length L is heated n degrees, it expands to a new length L'=(1+n*C)*L, where C is the coefficient of heat expansion. Whe

POJ 1905 Expanding Rods#二分

http://poj.org/problem?id=1905 题意:将一条直线变成一条弧线(该弧线是圆的一部分),求中心位置发生的位移. 由于精度需要控制好,所以选择用圆半径作为二分的目标,l=0,r=INF,LL为弧线长度,根据半径mid以及弦长L,可以求出对应的弧线长度t=2*asin(0.5*L/mid)*mid,再与LL比较,若t<LL,说明半径取大了,故r=mid,继续二分. #include<iostream> #include<cstdio> #include&

poj 1905 Expanding Rods 二分解方程

题意: 中已知L,S解h. 分析: 两个方程两个未知数,理论是可解的.解起来有困难,可用二分的方法. 代码: #include <iostream> #include <cmath> using namespace std; int main() { double l,n,c,s,r; while(scanf("%lf%lf%lf",&l,&n,&c)==3){ if(l<0) break; double mid,low=0.0,h

POJ 1064 Cable master 浮点数二分

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21181   Accepted: 4571 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

UVA 10668 - Expanding Rods(数学+二分)

UVA 10668 - Expanding Rods 题目链接 题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L′=(1+nc)l,问这时和原来位置的高度之差 思路:画一下图可以很容易推出公式,设圆弧扇形部弧度r,那么可以计算出铁棒长度为lr/sin(r)这个公式在[0, pi/2]是单调递增的,所以可以用二分法去求解 要注意的一点是最后答案计算过程中带入mid,之前是带入x(二分的左边值),可实际上x是可能等于0的,而带入mid,由于是double型,所以mid实际上表示是一个非常趋近0