打印n位内从1到最大的整数
def print1ToMaxNDigit(n):
if not isinstance(n, int):
raise TypeError("n must be an integer!")
if n < 1:
raise ValueError("n must be geeater than 0!")
tmp = [0] * n
while tmp != [9] * n:
i = 0
tmp = carry(tmp, i, n)
tmp_str = [str(c) for c in tmp]
rlt = "".join(tmp_str[::-1]).lstrip(‘0‘)
print(rlt)
def carry(tmp, i, n):
if i == n - 1 and tmp[i] == 9:
return tmp
elif tmp[i] + 1 < 10:
tmp[i] += 1
return tmp
else:
tmp[i] = 0
return carry(tmp, i+1, n)
原文:https://www.cnblogs.com/smoggy/p/10923498.html