-6

我想根据 n 对列表进行一些更改。例如:

l=[0,0,0,0,0,0,0,0,0,0,0,0] n=4 :

 print   l   #first iteration will print :[1,1,1,1,0,0,0,0,0,0,0,0]    # 4=1+1+1+1
 print   l   #next iteration will print :[1,1,0,2,0,0,0,0,0,0,0,0]    # 4=1+1+2
 print   l   #next iteration will print :[0,2,0,2,0,0,0,0,0,0,0,0]    # 4=2+2
 print   l   #next iteration will print :[1,0,0,3,0,0,0,0,0,0,0,0]    # 4=1+3
 print   l   #last iteration will print :[0,0,0,4,0,0,0,0,0,0,0,0]    # 4=4

列表中的序列对我来说无关紧要,但元素必须远离 =>9 应该是 sum==n.Rest 用零填充的每个可能的数字集

4

1 回答 1

0
l=[0,0,0,0,0,0,0,0,0,0,0,0]
n = 11

for i in range (0,n):
        l[i] = 1 

print l
carryidx = 0 
for i in reversed(range(0,n - 1)):
        v = l[ n - 1] + l[i]
        if v > 9:
                while l[carryidx] > 9:
                        carryidx += 1
                l[carryidx] += 1
        else:
                l[n - 1] = v 
        l[i] = l[i] - 1 
        print l

输出

[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
[1, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0, 0]
[1, 1, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0]
[1, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0]
于 2013-04-02T01:22:10.820 回答