筆者曾寫過利用遞迴實現不重複序列全排列的例子
現在給出另乙個利用進製實現的**:
sub pailie1(paramarray x())
dim starttime as single, endtime as single
dim n as integer, i as long, num as integer, j as integer
dim temp1() as string, temp2 as long, all as new collection
n = ubound(x) + 1 '元素個數
starttime = timer '開始計時
redim temp1(1 to n)
for i = 0 to n ^ n - 1
temp2 = i
set all = nothing
on error resume next
for j = n to 1 step -1
temp1(j) = x(temp2 mod n) '轉換為n進製
all.add temp1(j), temp1(j) '防止某一元素重複列出
temp2 = temp2 \ n
next
if all.count = n then
debug.print join(temp1, " ") '輸出
num = num + 1
end if
? doevents
next
endtime = timer
debug.print "共 " & num & " 種排列!用時 " & endtime - starttime & " 秒!"
end sub
private sub command1_click()
pailie1 "a", "b", "c", "d", "e", "f", "g"
end sub
非遞迴實現不重複序列的全排列(三)
筆者曾利用進製轉換實現不重複序列全排列 但從0 迴圈到n n 1 1,效率實在不高,經過仔細分析,發現乙個另人激動的規律,詳情見下表 a bacba dcba cdba cbda cbad bcad bcabdca bcda bcad bacd bacbdac badc bacdabc abdcab...
非遞迴實現不重複序列的全排列(三)
筆者曾利用進製轉換實現不重複序列全排列 但從0 迴圈到n n 1 1,效率實在不高,經過仔細分析,發現乙個另人激動的規律,詳情見下表 a ba cbadcba cdba cbda cbad bca dbca bdca bcda bcad bacd bac bdac badc bacdabc ab d...
非遞迴全排列實現
include include include include include include include includeusing namespace std const int max size 1000 尋找下乙個排列 假設 某個序列為 d1 d2 d3 dn 那麼在dn前找第乙個 比dn...