*!*
全排列 (SQL-Select 算法):
Clear
Set Talk Off
*!*
P(8,8) = 40320/2's, P(9,9) = 362880/11's, P(10,10) = 3628800/113's
? GenPermutation(10)
*!*
For I = 3 To 10
*!*
?? I
*!*
? GenPermutation( I )
*!*
Endfor
Function GenPermutation( pnSize )
Local I, lnStart, lcItem, lcDestination
lnStart = Seconds()
Create Cursor TheResult ( Permutation c(pnSize) )
Create Cursor ItemList ( Item C(1) )
For i = 1 To pnSize
Insert Into ItemList Values ( Chr( 65 + I - 1 ) )
Endfor
Select ItemList.Item + ItemListX.Item As Permutation ;
from ItemList ;
inner Join ItemList As ItemListX ;
on ItemList.Item != ItemListX.Item ;
into Cursor temp2
Select TheResult
Append From Dbf( "temp2" )
For I = 3 To pnSize
Create Cursor StepPermutation ( Permutation c(pnSize) )
Select ItemList
Scan
lcItem = ItemList.Item
Select lcItem + Permutation As Permutation ;
from TheResult ;
into Cursor Temp2 nofilter ;
where ! ( lcItem $ Permutation )
Select StepPermutation
Append From Dbf( "Temp2" )
Endscan
If I < pnSize
Select TheResult
Zap
Append From Dbf( "StepPermutation" )
Else
Select * ;
from StepPermutation ;
into Cursor TheResult
Endif
Endfor
Use In ItemList
Use In Temp2
Use In StepPermutation
Select TheResult
Return "P(" + Transform( pnSize ) + ") = " + Transform( Reccount() ) + " 组" + Chr(13) + Chr(10) + ;
"耗时 = " + Transform( Seconds() - lnStart, "99,999.99" ) + " 秒" + Chr(10)
Endfunc