zl程序教程

您现在的位置是:首页 >  Python

当前栏目

python老鼠打洞问题

2023-04-18 14:15:19 时间

老鼠打洞问题

描述:
有一堵十尺厚的墙,两只老鼠从两边向中间打洞。大老鼠第一天打一尺,小老鼠也是一尺。大老鼠每天的打洞进度是前一天的一倍,小老鼠每天的进度是前一天的一半。问它们几天可以相逢,相逢时各打了多少。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬
(注:本题禁止使用幂运算)‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬
输入格式:
输入1个整数,代表墙的厚度,单位为尺‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬
输出格式:
第一行输出1个整数,表示相遇时所需的天数‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬
第二行输出2个浮点数,分别为小鼠和大鼠打洞的距离,单位为尺,保留小数点后1位数字。

分析

分析题目中的未知项:
墙的厚度 n
大老鼠进度 已知第一天一尺
小老鼠进度 第一天一尺
大老鼠每天的打洞进度 已知是前一天的一倍
小老鼠每天的打洞进度 已知是前一天的一半
相逢需要时间
相逢时大老鼠打了多少 每天的打洞进度时间
相逢时小老鼠打了多少 每天的打洞进度
时间

实现

跟据题目描述,打洞的速度分为第一天和后几天,第一天统一一天一尺,那么在实现时也要区分只用一天就相遇,和需要好几天相遇。

n = int(input())
rat, mouse, day, time = 1, 1, 0, 1 #大老鼠进度,小老鼠进度,相遇时间,第一天时间
distance_of_rat, distance_of_mouse = 0, 0  # 大老鼠和小老鼠的打洞距离
while n > 0:
    if n - mouse - rat < 0: #第一天打洞完成
        time = n / (mouse + rat)	#算出需要时间
    n = n - mouse - rat	#剩余墙厚
    distance_of_mouse = distance_of_mouse + time * mouse
    distance_of_rat = distance_of_rat + time * rat
    rat = rat * 2	#大老鼠每天进度
    mouse = mouse / 2	#小老鼠每天进度
    day = day + 1	#时间过去一天
print(day)
print(round(distance_of_mouse, 1), round(distance_of_rat, 1))