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

UVA 10668 - Expanding Rods

题目链接

题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L=(1+nc)l,问这时和原来位置的高度之差

思路:画一下图能够非常easy推出公式,设圆弧扇形部弧度r,那么能够计算出铁棒长度为lr/sin(r)这个公式在[0, pi/2]是单调递增的,所以能够用二分法去求解

要注意的一点是最后答案计算过程中带入mid,之前是带入x(二分的左边值),可实际上x是可能等于0的,而带入mid,因为是double型,所以mid实际上表示是一个很趋近0的数字,而不是0.

代码:

#include <cstdio>
#include <cstring>
#include <cmath>

const double pi = acos(-1.0);

double l, n, c;

double cal(double r) {
    return l / sin(r) * r;
}

int main() {
    while (~scanf("%lf%lf%lf", &l, &n, &c)) {
	if (l < 0) break;
	double x = 0, y = pi / 2, lx = (1 + n * c) * l, m;
	for (int i = 0; i < 100; i++) {
	    m = (x + y) / 2;
	    if (cal(m) < lx) x = m;
	    else y = m;
	}
	printf("%.3lf\n", l / 2 / sin(m) * (1 - cos(m)));
    }
    return 0;
}


posted @ 2014-10-14 20:08  blfshiye  阅读(146)  评论(0编辑  收藏  举报