擅长:python、mysql、java
<p>您需要删除递归调用中完成的int类型转换,因为int('00')正在将数字转换为零(基本上删除所有起始零)并缩短字符串的长度。把那个铸件去掉。。其余代码正常工作:</p>
<pre><code>def decreasing(n):
asastring = str(n)
length = len(asastring)
# First the case where we need to add a digit.
if asastring == "9"*length:
return "1"+"0"*length
# Now we know that the next integer has the same number of digits as the original number.
if length == 1:
return int(n)+1
if length >= 2:
firstcharacter = asastring[0]
secondcharacter = asastring[1]
if int(firstcharacter) > int(secondcharacter):
return str(firstcharacter) + str(decreasing(asastring[1:]))
if int(firstcharacter) == int(secondcharacter):
return decreasing(firstcharacter+str(decreasing(asastring[1:])))
if int(firstcharacter) < int(secondcharacter):
return str(int(firstcharacter)+1)+'0'*(length-1)
</code></pre>