Expanding Rods

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;  
} 

 

 

DataFrame expanding 是用于在 Pandas 中执行 rollingexpanding 操作的方法之一。它可以用来计算滚动或扩展窗口中的统计数据,例如滚动平均值、滚动标准偏差等。 rolling 操作是指对于一个固定大小的窗口,沿着时间序列向前滑动,对窗口中的数据进行统计运算。而 expanding 操作是指从数据集的起始点开始,逐步扩大窗口的大小,对窗口中的数据进行统计运算。 下面是一个简单的示例,展示如何使用 expanding 方法计算累计和: ```python import pandas as pd # 创建一个包含随机整数的 DataFrame df = pd.DataFrame({'data': [1, 2, 3, 4, 5]}) # 使用 expanding 方法计算累计和 expanding_sum = df['data'].expanding().sum() print(expanding_sum) ``` 输出结果为: ``` 0 1.0 1 3.0 2 6.0 3 10.0 4 15.0 Name: data, dtype: float64 ``` 这里,我们使用 `expanding()` 方法创建了一个扩展窗口,并对 `data` 列进行累计求和。由于这是一个扩展窗口,因此在每一行中,我们都计算了从数据集起始点开始的所有值的累计和。 类似地,rolling 方法也可以用于计算滚动窗口内的统计数据。例如,以下代码演示了如何使用 rolling 方法计算一个窗口大小为 3 的滚动均值: ```python import pandas as pd # 创建一个包含随机整数的 DataFrame df = pd.DataFrame({'data': [1, 2, 3, 4, 5]}) # 使用 rolling 方法计算滚动均值 rolling_mean = df['data'].rolling(window=3).mean() print(rolling_mean) ``` 输出结果为: ``` 0 NaN 1 NaN 2 2.000000 3 3.000000 4 4.000000 Name: data, dtype: float64 ``` 这里,我们使用 `rolling()` 方法创建了一个大小为 3 的滚动窗口,并对 `data` 列进行滚动均值计算。在前两个行中,由于窗口大小不够,因此没有计算出平均值,因此这些行中的值为 NaN。在第三行中,我们计算了前三个值的平均值,并将其赋值给了第三个行。以此类推,直到最后一个行,我们计算了前五个值的平均值,得到了 4.0。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值