python运行时间过长怎么优化_我的短递归函数执行时间太长,我如何优化我

但是当我提交我的代码时,它显然需要很长时间来执行,并且说时间已经过期。我不确定我的代码是低效的(我觉得不是这样),还是I/O有问题。有9秒的时间限制,可以解决最多10个输入,0<=n<=1 000 000 000。在In Byteland they have a very strange monetary system.

Each Bytelandian gold coin has an integer number written on it. A coin

n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But

these numbers are all rounded down (the banks have to make a profit).

You can also sell Bytelandian coins for American dollars. The exchange

rate is 1:1. But you can not buy Bytelandian coins.

You have one gold coin. What is the maximum amount of American dollars

you can get for it?

这是我的代码:输入1000000000似乎花费了太长的时间def coinProfit(n):

a = n/2

b = n/3

c = n/4

if a+b+c > n:

nextProfit = coinProfit(a)+coinProfit(b)+coinProfit(c)

if nextProfit > a+b+c:

return nextProfit

else:

return a+b+c

return n

while True:

try:

n = input()

print(coinProfit(n))

except Exception:

break

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值