G - Expanding Rods LightOJ - 1137 (二分专题)。。

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. That means you have to calculate h as in the picture.

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case contains three non-negative real numbers: the initial length of the rod in millimeters L, the temperature change in degrees n and the coefficient of heat expansion of the material C. Input data guarantee that no rod expands by more than one half of its original length. All the numbers will be between 0 and 1000 and there can be at most 5 digits after the decimal point.

Output

For each case, print the case number and the displacement of the center of the rod in single line. Errors less than 10-6 will be ignored.

Sample Input

3

1000 100 0.0001

150 10 0.00006

10 0 0.001

Sample Output

Case 1: 61.3289915

Case 2: 2.2502024857

Case 3: 0

题意:一根长为L的细棒经加热升高n度,其长度伸长为L'=(1+n*C)*L,现在将其两端固定在墙上,求图中的h。

分析:此题用到很多数学知识。需要自己推导啊!首先说一下思路。用二分法枚举h高度的范围,从左端点0枚举至右端点R/2,右端点依题意可以确定。通过公式用枚举出的h计算得出该突出高度对应的膨胀后L'值,与题目中给出的L及伸长公式求出的L'相比较,若是求出的L'过大,则说明h取大了,求出的L'过小,说明h取小了。

注意:精度是将while循环的条件改为right-left>1e-6确定。这是一个技巧。。。。

由勾股定理R^{2}=(R-h)^{2}+(\frac{L}{2})^{2}可得R=\frac{L^2+4h^2}{8h}

L'=2\Theta R,对于R和L/2又有三角函数关系R=\frac{L}{2sin\Theta },变换得\Theta =arcsin\frac{L}{2R},与前式合并可得L'=2R*arcsin(\frac{L}{2R})

公式图片来自其他博客。。

#include <cstdio>
#include <cmath>
int main()
{
	double low,high,mid;
	double l,n,s,c;
	double r;
	int num = 1;
	int t;
	scanf ("%d",&t);
	for(int i=0;i<t;i++)
	{
		scanf ("%lf %lf %lf",&l,&n,&c);
		printf ("Case %d: ",num++);
		s = (1.0+n*c)*l;
		low = 0;
		high = l / 2.0;
		while (high - low > 1e-8)  //注意精度 
		{
			mid = (high + low) / 2.0;//double所以除以2。0 
			r = (l*l + 4*mid*mid) / (8*mid);
			double t = asin(l/2/r);
			if (t * r * 2.0 < s)
				low = mid;
			else
				high = mid;
		}
		printf ("%.6f\n",low);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42079027/article/details/81264712