奥数 --- 传令兵问题(第三届华中地区全国程序设计邀请赛)
Total Submit: 3 Accepted: 3 Special Judge: No
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?
First line of each case contains two values A and B. ( 1 <= A <= 1000000, 0 <= B <= 1000000 )
问:传令兵一共走了多少路?(传令兵始终匀速运动)
#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⊙)…
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3659368.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?