Non-recursive realization of unremnive sequences (3)

zhaozj2021-02-16  63

• The author has implemented non-repetitive sequences (http://blog.9cbs.net/northwolves/archive/2004/07/21/47400.aspx), but from 0 to N ^ (N-1 ) -1, the efficiency is not high. After careful analysis, I found another exciting law. See the table below for details:

?

A

?

BA

CBA

DCBA

CDBA

CBDA

CBAD

BCA

DBCA

BDCA

BCDA

BCAD

Bac

DBAC

BDAC

Badc

Bacd

AB

Cab

DCAB

CDAB

CADB

CABD

ACB

DACB

ADCB

ACDB

ACBD

ABC

DABC

ADBC

ABDC

ABCD

As can be seen from the table, for "abcd", if you put a (only one means), when putting B, there can be BA, AB, and when it is placed, it is for BA, AB. Each of the three kinds of implant (before, Ba, Ba), and then in addition to D, there are four kinds of letters. Therefore, after the first element is ranked, the position of the second element can be represented by 0, 1, and the position of the third element can be represented by 0, 1, 2, and the position of the nth element can be used 0, 1, 2. , 3, ... N-1, thus use a mixed basis (the name of the author) can realize the full alignment of array elements.

code show as below:

Sub Pailie3 (Paramarray X ()) Dim StartTime As Single, EndTime As Singledim I AS Integer, J AS Integer, Num As New Collection, Temp1 As Long, Temp2 As Longn = Ubound (x) 1 'Element number startTime = Timer' Start timing Num = 1for i = 1 to nnum = NUM ​​* I? 'Recursion calculation N! Nextfor i = 1 to NumSet all = nothing 'initialization collection allall.add x (0) temp1 = ifor j = 2 to ntemp2 = Temp1 mod jtemp1 = temp1 / jif temp2 = 0 theall.add x (j - 1)' TEMP2 is 0 placed in the last Elseall.Add x (J - 1),, before: = TEMP2 'TEMP2 is not 0 End ifnextfor J = 1 to ndebug.print all (j) & ";" 'Outgoing NextdeBug.printnextendTime = TimerDebug.print "Total" & Num & "Area! That" & EndTime - StartTime & "Second!" End Sub

PRIVATE SUB Command1_Click () Pailie3 "A", "B", "C", "D", "E", "F", "G" End Sub

?

Since the collection belongs to the Variant type, the calculation speed is slow, the same conversion is changed to the array, and it is indeed a lot of fast: Sub Pailie4 (paramarray x ()) Dim StartTime As Single, EndTime As Singledim I as Integer, J AS Integer, k as Integer, Num As Long (), TEMP1 As long (), TEMP1 AS LONG, TEMP2 AS LONGN = Ubound (x) 1 'Element

StartTime = Timer 'Start Timing Num = 1for i = 1 to nnum = NUM ​​* I?' recursive calculation N! Nextfor i = 1 to Numredim all (1 to n) 'initialization array arlall (1) = x (0) Temp1 = IFOR J = 2 to ntemp2 = Temp1 mod jtemp1 = temp1 / jif temp2 = 0 THENALL (J) = X ( J - 1) 'Temp2 is 0 placed in the last Elsefor K = J to Temp2 1 Step -1all (k) = all (k - 1)?' Temp2 Elements After the element is moved back (TEMP2) = X (TEMP2) = X ( J - 1) 'TEMP2 is not 0 End ifnextdebug.print Join (ALL, ")?' Output NEXTENDTIME = TimerDebug.print" & Num & "Arrangement! Time" & EndTime - StartTime & "Second!" End SubPrivate Sub Command1_Click () Pailie4 "A", "B", "C", "D", "E", "F", "G" end sub

If you use CopyMemory to move, speed should be faster, everyone is interested in trying.

转载请注明原文地址:https://www.9cbs.com/read-14456.html

New Post(0)