下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
组合算法 本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标 代表的数被选中,为0则没选中。 首先...
group = [1,1,0]
group_len = len(group)
#计算次数
ret = [group]
ret_num = (group_len * (group_len - 1) * (group_len - 2)) / 6
for i in xrange(ret_num - 1):
'第一步:先把10换成01'
number1_loc = group.index(1)
number0_loc = group.index(0)
#替换位置从第一个0的位置开始
location = number0_loc
#判断第一个0和第一个1的位置哪个在前,
#如果第一个0的位置小于第一个1的位置,
#那么替换位置从第一个1位置后面找起
if number0_loc < number1_loc:
location = group[number1_loc:].index(0) + number1_loc
group[location] = 1
group[location - 1] = 0
'第二步:把第一个10前面的所有1放在数组的最左边'
if location - 3 >= 0:
if group[location - 3] == 1 and group[location - 2] == 1:
group[location - 3] = 0
group[location - 2] = 0
group[0] = 1
group[1] = 1
elif group[location - 3] == 1:
group[location - 3] = 0
group[0] = 1
elif group[location - 2] == 1:
group[location - 2] = 0
group[0] = 1
print group
ret.append(group)
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。