问题描述
我正在尝试在 CodeWars 上做“买车”卡塔,但我无法获得正确的“可用”值, 这是我需要做的
一个男人有一辆价值 2000 美元的旧车。他看到一辆价值8000美元的二手车。他想保留他的旧车,直到他能买到二手车。
他认为他每个月可以节省 1000 美元,但他的旧车和新车的价格每月下降 1.5%。此外,这种损失百分比在每两个月结束时增加 0.5%。我们的人发现很难进行所有这些计算。
他需要多少个月才能存够钱买他想要的车,还有多少钱?
这是我的解决方案:
def nbMonths(startPriceOld,startPriceNew,savingperMonth,percentLossByMonth):
months = 0
savings = savingperMonth*months
price_new = startPriceNew
price_old = startPriceOld
money = price_old + savings
percent = percentLossByMonth
while money <= price_new:
months = months + 1
price_new = startPriceNew-(startPriceNew*(percent/100)*months)
price_old = startPriceOld-(startPriceOld*(percent/100)*months)
savings = savingperMonth*months
money = price_old + savings
available = money - price_new
if months % 2 == 0:
percent = percentLossByMonth + (0.5 * months/2)
print(months)
print("money",money)
print("savings",savings)
print("price_new",price_new)
print("price_old",price_old)
print("percent",percent)
print("available",available)
available = money - price_new
return [months,available]
使用这些参数进行测试
nbMonths(2000,8000,1000,1.5)
到 6 个月末我得到回报
[6,900]
但正确的是
[6,766]
我做错了什么?我试图在月底和开始时增加百分比,但没有帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)