UVALive 7292 Refract Facts(二分法)

本文介绍了一种通过二分法求解潜艇使用激光通讯时,针对空中目标所需调整的激光发射角度的方法。考虑到激光在水与空气界面上的折射效应,利用斯涅尔定律计算角度。文章提供了一个C++实现的示例,展示了如何根据潜艇深度、飞机高度及距离等参数,精确计算激光发射的仰角。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Refract Facts

A submarine is using a communications laser to send a message to a jet cruising overhead. The sea surface is flat. The submarine is cruising at a depth d below the surface. The jet is at height h above the sea surface, and a horizontal distance x from the sub. The submarine turns toward the jet before starting communications, but needs to know the angle of elevation, φ, at which to aim the laser.

refract.png

When the laser passes from the sea into the air, it is refracted (its path is bent). The refraction is described by Snell's law, which says that light approaching the horizontal surface at an angle θ1, measured from the vertical, will leave at an angle θ2, given by the formula

sin θ1 / sin θ2 = n1 / n2

where n1 and n2 are the respective refraction indices of the water and air. (The refraction index of a material is inversely proportional to how fast light can travel through that material.)

snell.png

Input

Each test case consists of a single line of input containing 5 space-separated floating point numbers:

  • d, the depth of the submarine (specifically, of the laser emitter) in feet, 1 <= d <= 800
  • h, the height of the plane in feet, 100 <= h <= 10,000
  • x, the horizontal distance from the sub to the plane in feet, 0 <= x <= 10,000
  • n1, the refractive index of water, 1.0 < n1 <= 2.5
  • n2, the refractive index of air, 1.0 <= n2 < n1

Input ends with a line containing 5 zeroes (0 0 0 0 0).

Output

For each test case, print a single line containing the angle of elevation φ at which the submarine should aim its laser to illuminate the jet.

The angle should be displayed in degrees and rounded to the closest 1/100 of a degree. Exactly two digits after the decimal point should be displayed.

Sample Input

600 600 1000 1.333 1.01
600 1200 4000 1.5 1.01
400 100 10000 2.5 1.01
0 0 0 0 0

Sample Output

44.37
11.51
2.30

这道题就是要二分求值,对θ1的角度进行二分,范围在0-90度,所求的角度φ为90-θ1,可以得到公式h*tan(θ2)+d*tan(θ1)=x,

关键是求出θ1和θ2的值。asin(sinθ2)=θ2的角度。如asin(sinπ/2)=45° .

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define PI acos(-1.0)
#define eps 1e-6
using namespace std;

int main()
{
    double d,h,x,n1,n2;
    while(~scanf("%lf %lf %lf %lf %lf",&d,&h,&x,&n1,&n2))
    {
        if(d==0&&h==0&&d==x&&h==n1&&n2==0)
            break;
        double l=0,r=90,ang1,ang2,mid;
        while(l+eps<=r)
        {
            mid=(l+r)/2;
            ang1=mid/180*PI;
            ang2=asin(sin(ang1)*n2/n1);
            if(h*tan(ang2)+d*tan(ang1)<=x)
                l=mid;
            else
                r=mid;
        }
        printf("%.2lf\n",90-r);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值