[清華集訓2015]燈泡(浙江大學ZOJ 3203 Light Bulb)

Time Limit: 1 Second      Memory Limit: 32768 KB算法

Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow and he has only one light bulb in his house. Every night, he is wandering in his incommodious house, thinking of how to earn more money. One day, he found that the length of his shadow was changing from time to time while walking between the light bulb and the wall of his house. A sudden thought ran through his mind and he wanted to know the maximum length of his shadow.函數

Input測試

The first line of the input contains an integer T (T <= 100), indicating the number of cases.spa

Each test case contains three real numbers Hh and D in one line. H is the height of the light bulb while h is the height of mildleopard. D is distance between the light bulb and the wall. All numbers are in range from 10-2 to 103, both inclusive, and H - h >= 10-2..net

Output3d

For each test case, output the maximum length of mildleopard's shadow in one line, accurate up to three decimal places..code

Sample Inputblog

3
2 1 0.5
2 0.5 3
4 3 4

Sample Outputthree

1.000
0.750
4.000

題目連接ci

題目大意:

他的房子狹窄,他家裏只有一個燈泡。天天晚上,他都在他不知名的房子裏徘徊,想着如何賺更多的錢。有一天,他發現他的影子長度在燈泡和房子的牆壁之間行走時不時變化。忽然想到了他的思緒,他想知道他的影子的最大長度。

輸入的第一行包含整數TT <= 100),表示個案數。

每一個測試用例在一行中 包含三個實數HhD. H是燈泡的高度,h是輕度高度的高度。 D是燈泡和牆壁之間的距離。全部數字的範圍均爲10 -2至10 3,包括二者,H - h > = 10 -2

對於每一個測試用例,在一行中輸出mildleopard陰影的最大長度,精確到三位小數。

算法分析

參考:

https://blog.csdn.net/Mrx_Nh/article/details/52745348    分析理解應該是對的,可是代碼有誤,估計是沒有討論影子不落到牆上的狀況。

https://blog.csdn.net/qq_38944163/article/details/80870928  代碼是正確的。

這個題要分兩種狀況討論:影子沒有落到牆上;影子落到牆上。

影子沒有落到牆上的狀況以下圖所示:

影子落到牆上的狀況以下圖所示

 

算影子的長度能夠用類似三角形,相信各位上過初中的朋友都會。。

那麼咱們發現這是一個單峯函數,要求峯值,這裏咱們能夠用一個很強大的東西,三分法。

具體看代碼吧

 1 #include<stdio.h>
 2 double f(double x,double H,double h,double D)
 3 {
 4     double tanA,Y;
 5     tanA=(H-h)/x;//這裏你們能夠畫個圖幫助理解
 6     Y=H/tanA;//下面那條的長(在沒有牆的狀況下)
 7     if(Y<=D) return Y-x;//若是沒到牆,就直接返回地上影子的長度。
 8     Y=Y-D;
 9     return D-x+Y*tanA;//D-X是地上影子的長度,Y*tanA是牆上影子的長度.
10 }
11 int main()
12 {
13     freopen("p3382.in","r",stdin);
14     int i,T;
15     double H,h,D;
16     double L,R,m1,m2,fm1,fm2;
17     scanf("%d",&T);
18     for(i=0;i<T;i++)
19     {
20         scanf("%lf%lf%lf",&H,&h,&D);
21         L=0;R=D;
22         while(L+1e-11<R)
23         {
24             m1=L+(R-L)/3; m2=R-(R-L)/3;
25             fm1=f(m1,H,h,D);  fm2=f(m2,H,h,D);
26             if(fm1<fm2) L=m1;
27             else R=m2;
28         }
29         printf("%.3lf\n",f(L,H,h,D));
30     }
31     return 0;
32 }

 

爲什麼是一個單峯,能夠考慮看一下第一個參考的博客。

理解時參考下圖:

相關文章
相關標籤/搜索