演算法實驗報告1——全排列——2020.12.17
阿新 • • 發佈:2020-12-21
技術標籤:python演算法-每日一練演算法python資料結構leetcode字串
演算法實驗報告1——全排列
一丶全排列程式碼
# coding=gbk
__author__ = 'Blockchain_Key'
__time__ = '2020.12.17'
# itertools模組現成的全排列:
import itertools as it
def AllRange1(s):
for i in it.permutations(s,len(s)):
print (''. join(i))
def AllRange2(head="",string=""):
if len(string)>1:
for father_string in string:
#關鍵一點:將頭和尾全部傳下去
AllRange2(head+father_string,string.replace(father_string,""))
else:
print(head+string)
if __name__ == "__main__" :
AllRange1(input('請輸入全排列的數:'))
AllRange2(string=input('請輸入全排列的數:'))