The role of Keqing, Yuheng of the Liyue Qixing, is to manage real estate and construction in Liyue.Today Keqing decides to leave her office and visit two places - Chasm, and Jueyun Karst - to make sure the building progress is at a reasonable pace.The location of Keqing’s office and the two places to visit can be denoted as three points on a 2D Cartesian coordinate system.
Keqing’s office, the start point, is located at point O (0, 0).
Chasm is located at point A (a, b).
Jueyun Karst is located at point B (2a, 0).
Keqing plans to visit Chasm first and then go to Jueyun Karst. Because Keqing is an experienced land overseer, she does not need to be at the exact location to do her job. She can finish the visit if the distance between her and the target point is no greater than R.
Please help Keqing find the shortest path to visiting Chasm and Jueyun Karst.
Input
There are T test cases in this problem.
The first line has one integer T.
In the following T lines, each contains three integers a, b, R.
We guarantee that 0 < a, b, R < 109, a^2 + b^2 > 4R^2, and 0 < T ≤ 1000.
Output
For each test case, you should first print “Case #t: ” (without quotes), where t is the index of this test case.
Then in the same line, print a number L, the length of the shortest path to finish the visit, rounded to two decimal places.
Example
standard input
1
3 5 1
standard output
Case #1: 9.00
Note
In the sample input, we have Chasm at (3,5), Jueyun Karst at (6,0), and Keqing can finish the visiting within radius 1.
So Keqing could go directly to point (3,4) first to visit Chasm, then reach (5.4,0.8) to visit Jueyun Chasm.
The total length is 5+4=9. We can prove that there is no path with a smaller distance.
题意
输入三个数a b r
三个点分别是X(0,0),Y(a,b), Z(2a,0)
求X到 距离Y不大于r的点的距离再到距离Z不大于r的点的距离的最小值
思路
距离某点的距离不大于R就是以某点为圆心R为半径的圆的范围内的点
先找出第二个点所在的圆离原点最近的那个点,其实就是(a,b-r),判断b-r的正负,如果是正,最短距离就是原点到(a,b-r)的距离乘2-r,因为是等腰三角形;如果为负,说明圆一定包含了x轴的一部分,所以距离原点最近的那个点就认为是在x轴上,所以最短距离就是2*a-r
代码
#include<stdio.h>
#include<math.h>
int main()
{
int t;
scanf("%d",&t);
int k=0;
while(t--)
{
k++;
double a,b,r;
scanf("%lf %lf %lf",&a,&b,&r);
double s3;
if(b-r>0)
{
double s1=sqrt(a*a+(b-r)*(b-r));
s3=2*s1-r;
}
else
s3=2*a-r;
printf("Case #%d: %.2f\n",k,s3);
}
return 0;
}