Next highest int from digits: Difference between revisions
Content added Content deleted
(Add C# implementation) |
(Added FreeBASIC) |
||
Line 1,027: | Line 1,027: | ||
9589776899767587796600 -> 9589776899767587900667 |
9589776899767587796600 -> 9589776899767587900667 |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
===algorithm 1=== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="vbnet">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."</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0 => 0 |
|||
9 => 0 |
|||
12 => 21 |
|||
21 => 0 |
|||
12453 => 12534 |
|||
738440 => 738440 |
|||
45072010 => 45072010 |
|||
95322020 => 95322020 |
|||
67.04065610002726sec.</pre> |
|||
===algorithm 2=== |
|||
{{trans|Phix}} |
|||
<syntaxhighlight lang="vbnet">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."</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0 => 0 |
|||
9 => 0 |
|||
12 => 21 |
|||
21 => 0 |
|||
12453 => 12534 |
|||
738440 => 740344 |
|||
45072010 => 45072000 |
|||
95322020 => 95322000 |
|||
9589776899767587796600 => 9589776899767587900667 |
|||
0.004686999949626625sec.</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |