-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path打印1到最大的n位数.py
67 lines (55 loc) · 1.52 KB
/
打印1到最大的n位数.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''
输入数字n, 按顺序打印从1最大的n位十进制数
比如输入3, 则打印出1、2、3、到最大的3位数即999
'''
def Print1ToMaxOfNDigits(n):
if n <= 0:
return
number = ['0'] * n
while not Increment(number):
PrintNumber(number)
def Increment(number):
isOverflow = False
nTakeOver = 0
nLength = len(number)
for i in range(nLength-1, -1, -1):
nSum = int(number[i]) + nTakeOver
if i == nLength - 1:
nSum += 1
if nSum >= 10:
if i == 0:
isOverflow = True
else:
nSum -= 10
nTakeOver = 1
number[i] = str(nSum)
else:
number[i] = str(nSum)
break
return isOverflow
def PrintNumber(number):
isBeginning0 = True
nLength = len(number)
for i in range(nLength):
if isBeginning0 and number[i] != '0':
isBeginning0 = False
if not isBeginning0:
print('%c' % number[i], end='')
print('')
#
# Print1ToMaxOfNDigits(2)
def Print1ToMaxOfNDigits2(n):
if n <= 0:
return
number = ['0'] * n
for i in range(10):
number[0] = str(i)
Print1ToMaxOfNDigitsRecursively(number, n, 0)
def Print1ToMaxOfNDigitsRecursively(number, length, index):
if index == length - 1:
PrintNumber(number)
return
for i in range(10):
number[index + 1] = str(i)
Print1ToMaxOfNDigitsRecursively(number, length, index+1)
Print1ToMaxOfNDigits2(2)