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. 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
红色为杆弯曲后,长度为s
h是所求
依题意知
S=(1+n*C)*L
又从图中得到三条关系式;
(1) 角度→弧度公式 θr = 1/2*s
(2) 三角函数公式 sinθ= 1/2*L/r
(3) 勾股定理 r^2 – ( r – h)^2 = (1/2*L)^2
把四条关系式化简可以得到
逆向思维解二元方程组:
要求(1)式的h,唯有先求r
但是由于(2)式是三角函数式,直接求r比较困难
因此要用顺向思维解方程组:
在h的值的范围内枚举h的值,计算出对应的r,判断这个r得到的(2)式的右边 与 左边的值S的大小关系 ( S= (1+n*C)*L )
很显然的二分查找了。。。。。
那么问题只剩下 h 的范围是多少了
下界自然是0 (不弯曲)
关键确定上界
题中提及到
Input data guarantee that no rod expands by more than one half of its original length.
意即输入的数据要保证没有一条杆能够延伸超过其初始长度的一半
就是说 S max = 3/2 L
理论上把上式代入(1)(2)方程组就能求到h的最小上界,但是实际操作很困难
因此这里可以做一个范围扩展,把h的上界扩展到 1/2L ,不难证明这个值必定大于h的最小上界,那么h的范围就为 0<=h<1/2L
这样每次利用下界low和上界high就能得到中间值mid,寻找最优的mid使得(2)式左右两边差值在精度范围之内,那么这个mid就是h
精度问题是必须注意的
由于数据都是double,当low无限接近high时, 若二分查找的条件为while(low<high),会很容易陷入死循环,或者在得到要求的精度前就输出了不理想的“最优mid”
精度问题也可以令一个数50左右,让他循环50次,也能求出结果
#include<iostream>
#include<math.h>
#include<cstdio>
using namespace std;
const double esp=1e-8; //最低精度限制
int main(void)
{
double L,n,c,s; //L:杆长 ,n:温度改变度 , c:热力系数 ,s:延展后的杆长(弧长)
double h; //延展后的杆中心 到 延展前杆中心的距离
double r; //s所在圆的半径
int t,cas=0;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf",&L,&n,&c);
if(L<0 && n<0 && c<0)
break;
double low=0.0; //下界
double high=0.5*L; // 0 <= h < 1/2L (1/2L并不是h的最小上界,这里做一个范围扩展是为了方便处理数据)
double mid;
s=(1+n*c)*L;
while(high-low>esp) //由于都是double,不能用low<high,否则会陷入死循环
{ //必须限制low与high的精度差
mid=(low+high)/2;
r=(4*mid*mid+L*L)/(8*mid);
if( 2*r*asin(L/(2*r)) < s ) //h偏小
low=mid;
else //h偏大
high=mid;
}
h=mid;
printf("Case %d: %.7lf\n",++cas,h);
}
return 0;
}