某个兔子饲养场,兔子体重达到10斤就可以售卖,兔子的体重按照每月平均15%的比例增长,从键盘上输入兔子的体重,请问几个月后兔子可以售卖
要求输入兔子的体重为浮点数
输出月数为正整数
import math
weight = float(input())
months = math.ceil(math.log(10 / weight, 1.15))
print(months)
问题解决方案:
根据题意,可以列出体重增长公式:新体重=原体重×(1+0.15)^月份
将新体重等于10斤的方程式化解即可得出答案。
具体步骤如下:
weight = float(input("请输入兔子的当前体重(单位:kg):"))
sell_weight = 10 * 0.5
import math
months = 0 # 初始月份为0
while True:
weight *= 1.15
months += 1
if weight >= sell_weight:
break
print("需要%d个月才能达到可售卖的体重。" % months)
完整代码如下:
weight = float(input("请输入兔子的当前体重(单位:kg):"))
sell_weight = 10 * 0.5
import math
months = 0 # 初始月份为0
while True:
weight *= 1.15
months += 1
if weight >= sell_weight:
break
print("需要%d个月才能达到可售卖的体重。" % months)
输出结果(样例):
请输入兔子的当前体重(单位:kg):2.6 需要9个月才能达到可售卖的体重。
import math
weight = input("请输入兔子的体重(单位:斤):")
if not weight.isdigit() and not weight.replace('.', '').isdigit():
print("ERROR")
else:
weight = float(weight)
month = math.ceil(math.log(10/weight, 1.15))
print("兔子需要{}个月才能出笼".format(month))