小青蛙爬井问题:计算爬上井所需天数 - Python代码实现
"关于小青蛙爬井的故事,你应该早就听过了:井深10尺,小青蛙从井底向上爬,每个白天向上爬3尺,每个晚上又滑下来2尺,然后问你第几天它能爬上来。答案是第8天。 \n\n现在,那只著名的小青蛙又回来了,它现在每个白天已经可以向上爬m(2 <= m <=10)尺了,当然,晚上还是要下滑n(1 <= n < m )尺的。如果告诉你井深h(10 <= h <= 200)尺,请计算一下,现在,它第几天可以爬上来。有三个整数,分别表示 m、 n、 h。\n\n用py做内容:你可以使用以下代码来计算小青蛙爬上来所需的天数:\n\npython\ndef calculate_days(m, n, h):\n\t height = 0\n\t days = 0\n\t while height < h:\n\t\t height += m\n\t\t days += 1\n\t\t if height >= h:\n\t\t\t break\n\t\t height -= n\n\t return days\n\nm = int(input("请输入每天向上爬的距离m(2 <= m <= 10):"))\n n = int(input("请输入每晚下滑的距离n(1 <= n < m):"))\n h = int(input("请输入井的深度h(10 <= h <= 200):"))\n\n result = calculate_days(m, n, h)\n print("小青蛙需要", result, "天才能爬上来")\n\n\n你可以根据题目给定的条件,输入对应的m、n和h的值,然后运行代码即可得到结果。
原文地址: https://www.cveoy.top/t/topic/pX33 著作权归作者所有。请勿转载和采集!