1.保持a不动,动bcd
2.保持b不动,动cd
3.保持c不动,动d

def pailie(head="",string=""):
    if len(string)>1:
        for father_string in string:
            pailie(head+father_string,string.replace(father_string,"")) #关键一点:将头和尾全部传下去
    else:
        print(head+string)


pailie(string="abcd")

与上一个两个不同,一是,第一个a排完顺序后,下一个a不能再排,二是替换的时候不能把重复的也替换掉

def pailie(head="",string=""):
    if len(string)>1:
        for num,father_string in enumerate(string):
            if father_string in string[0:num]:#如果字符与前面的重复说明拍过顺序了
                continue
            pailie(head+father_string,string.replace(father_string,"",1))#只能替换一次
    else:
        print(head+string)

pailie(string="abca")

相关文章:

  • 2021-08-22
  • 2022-12-23
  • 2021-09-27
  • 2021-09-24
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-01
  • 2021-07-14
  • 2022-12-23
  • 2021-09-27
  • 2021-11-24
相关资源
相似解决方案