One day Ryan picked up a dog, a very naughty and lively dog. He named it Rainer.
Rainer the dog loves running here and there, and chases everything under its sight. Another 'one day', Ryan and Rainer took a slow walk in a fragrant garden.
While Ryan was indulged in the million colors of flowers, Rainer catched plenty of butterflies flying in a line overhead, as usual, it tried to chase them again.
Assume the line of butterflies were a definitely straight line, and Rainer the dog and the butterflies moved in the same direction. Rainer started at the tail of the butterflies line, after it moved to the head of the line, it turned around immediately, moved to the tail again. In this period, they all moved with a different but constant speed.
If the length of butterflies line is A miles, and the line moved B miles in the litter chasing game, how long had Rainer moved in total?
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main() { int A,B; while(cin>>A>>B!=NULL) { double ans; ans=(double)A+sqrt(A*A+B*B); printf("%.2lf\n",ans); } return 0; }
另附:
【推导过程】
设,队速为S,兵速为r*s;队伍长L
则,兵追到队首的时间为t1 = L/(r-1)s
兵再折返到队尾的时间为t2 = L/(r+1)s
总时间t =t1+t2=L/s * 2r/(r^2-1)
队行总距离D1 =t*s= L * 2r/(r^2-1) => r^2 - 2(L/D1)r - 1=0
=> r = (L + sqrt(L^2 + D1^2) )/ D1
兵行总距离D2=t*r*s=D1*r= L + sqrt(L^2 + D1^2)
另外一种神推理:(数学不好的可以忽略)
用勾股定理去解,方程实在复杂了点:
如果把队伍前进1米所用时间看成一个时间单位,则总时间为50;
假设传令兵的速度为x,那么传令兵绕1圈的路程为:50x;
无论从何地出发,绕1圈用的时间相同。
我们假设传令兵从队伍的后角开始出发:
第一段:是一个追及问题:
路程差为50m,速度差为:x-1,所用时间为:50/(x-1);
第三段:是一个相遇问题:
路程和为50m,速度和为:x+1,所用时间为:50/(x+1);
第二段与第四段:
当传令兵在队伍前面或队伍后面时,传令兵行走路线分别如图所示:
总时间减去第一段与第三段的时间,则为前后时间和:
50-50/(x-1)-50/(x+1)
队伍前进的速度为1,传令兵的速度为x,可知:
队伍前进的路程即下面直角边长度为:50-50/(x-1)-50/(x+1)
传令兵前进的路程即斜边的长度为:[50-50/(x-1)-50/(x+1)]*x
根据勾股定理:两直角边的平方和=斜边的平方,可得方程。再求x的值。
而50x即为传令兵绕1圈的路程。
看这架势,方程很复杂,是个四次方程。谁愿意解谁就去解。
可以看出这种方法显然在程序设计中会很麻烦,所以说这种方法不是为算法而设计的(⊙o⊙)…