高一 python 思维练习 - 非递归组合问题
从列表中 L=[“a”,“b”,“c”,“d”,“e”,“f”,“g”] 选取4个元素,使用python非递归的,回溯算法
L=["a","b","c","d","e","f","g"]
v=[0,0,0,0,0,0,0]
R=[]
i=0
item=0
while 1:
if v[i]==0:
R.append(i)
v[i]=1
if len(R)==4:
item+=1
t=[]
for x in R:
t.append(L[x])
print(item,":",t)
R.pop()
if i<len(L):
i+=1
if i==len(L):
if len(R)==0:
break;
else:
i=R.pop()
for y in range(i,len(L)-1):
v[y+1]=0