1. 程式人生 > 其它 >演算法實驗報告1——全排列——2020.12.17

演算法實驗報告1——全排列——2020.12.17

技術標籤: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('請輸入全排列的數:'))

二丶執行結果

在這裡插入圖片描述

——虎鼓瑟兮鸞回車,仙之人兮列如麻——