Data update
This commit is contained in:
parent
0df55f9f24
commit
aec8ed51b6
1045 changed files with 18889 additions and 2777 deletions
|
|
@ -0,0 +1,62 @@
|
|||
Function factorial(n As Integer) As Uinteger
|
||||
Return Iif(n = 0, 1, n * factorial(n - 1))
|
||||
End Function
|
||||
|
||||
Sub swap_(s As String, i As Integer, j As Integer)
|
||||
Dim As String temp = Mid(s, i, 1)
|
||||
Mid(s, i, 1) = Mid(s, j, 1)
|
||||
Mid(s, j, 1) = temp
|
||||
End Sub
|
||||
|
||||
Sub permute(s As String, l As Integer, r As Integer, perms() As String)
|
||||
If l = r Then
|
||||
Redim Preserve perms(Ubound(perms) + 1)
|
||||
perms(Ubound(perms)) = s
|
||||
Else
|
||||
For i As Uinteger = l To r
|
||||
swap_(s, l, i)
|
||||
permute(s, l + 1, r, perms())
|
||||
swap_(s, l, i) ' backtrack
|
||||
Next i
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Sub bubbleSort(arr() As String)
|
||||
Dim As Integer i, j, n = Ubound(arr)
|
||||
Dim As String temp
|
||||
|
||||
For i = 0 To n - 1
|
||||
For j = 0 To n - i - 1
|
||||
If arr(j) > arr(j + 1) Then
|
||||
temp = arr(j)
|
||||
arr(j) = arr(j + 1)
|
||||
arr(j + 1) = temp
|
||||
End If
|
||||
Next j
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Function nextHigh1(Byref n As String) As String
|
||||
Dim As String perms()
|
||||
Dim As Uinteger i, idx
|
||||
|
||||
permute(n, 1, Len(n), perms())
|
||||
bubbleSort perms()
|
||||
Dim As Uinteger k = Ubound(perms)
|
||||
|
||||
For i = 0 To k
|
||||
If perms(i) = n Then
|
||||
idx = i
|
||||
Exit For
|
||||
End If
|
||||
Next i
|
||||
|
||||
Return Iif(idx < k, perms(idx + 1), "0")
|
||||
End Function
|
||||
|
||||
Dim As String tests1(7) = {"0", "9", "12", "21", "12453", "738440", "45072010", "95322020"}
|
||||
Dim As Double t0 = Timer
|
||||
For i As Uinteger = 0 To Ubound(tests1)
|
||||
Print tests1(i); " => "; nextHigh1(tests1(i))
|
||||
Next i
|
||||
Print Chr(10); Timer - t0; "sec."
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
Function sort(s As String) As String
|
||||
Dim As Uinteger i, j, n = Len(s)
|
||||
Dim As String temp
|
||||
|
||||
For i = 1 To n
|
||||
For j = i + 1 To n
|
||||
If Asc(Mid(s, i, 1)) > Asc(Mid(s, j, 1)) Then
|
||||
temp = Mid(s, i, 1)
|
||||
Mid(s, i, 1) = Mid(s, j, 1)
|
||||
Mid(s, j, 1) = temp
|
||||
End If
|
||||
Next j
|
||||
Next i
|
||||
|
||||
Return s
|
||||
End Function
|
||||
|
||||
Function rfind(c As String, s As String) As Uinteger
|
||||
Return Instr(s, c)
|
||||
End Function
|
||||
|
||||
Function nextHigh2(n As String) As String
|
||||
Dim As Uinteger hi = Asc(Right(n, 1))
|
||||
Dim As Uinteger i, ni, idx
|
||||
Dim As String sr
|
||||
|
||||
For i = Len(n) - 1 To 1 Step -1
|
||||
ni = Asc(Mid(n, i, 1))
|
||||
If ni < hi Then
|
||||
sr = sort(Mid(n, i))
|
||||
idx = rfind(Chr(ni), sr) + 1
|
||||
Mid(n, i, 1) = Mid(sr, idx, 1)
|
||||
Mid(sr, idx, 1) = ""
|
||||
Mid(n, i + 1) = sr
|
||||
Return n
|
||||
End If
|
||||
hi = Iif(hi > ni, hi, ni)
|
||||
Next i
|
||||
|
||||
Return "0"
|
||||
End Function
|
||||
|
||||
Dim As String tests2(8) = { "0", "9", "12", "21", "12453", _
|
||||
"738440", "45072010", "95322020", "9589776899767587796600" }
|
||||
Dim As Double t1 = Timer
|
||||
For i As Uinteger = 0 To Ubound(tests2)
|
||||
Print tests2(i); " => "; nextHigh2(tests2(i))
|
||||
Next i
|
||||
Print Chr(10); Timer - t1; "sec."
|
||||
Loading…
Add table
Add a link
Reference in a new issue