我有一个vb6函数,最多需要7个数字,订购它们,找到一个共同的区别。这个数字序列将存在差距。我还想确定丢失的号码。
示例输入为19,17,20,and 16
。输出应按此顺序为数组16,17,18,19,20
。有帮助吗?我可能能够将vb.net代码解释为vb6,但vb6对我来说是首选。
答案 0 :(得分:1)
以下是需要做的事情的概述:
请注意,您根本不需要对数字进行排序。你只需要找到极值。
这是VB6中的代码大纲:
Function RangeFrom(ParamArray Numbers() As Long) As Long()
Dim Lowest As Long
Dim Highest As Long
Lowest = Numbers(0)
Highest = Numbers(0)
Dim Number As Long
For Each Number In Numbers
If Number < Lowest Then Lowest = Number
If Number > Highest Then Highest = Number
Next
Dim Result(0 To Highest - Lowest) As Long
Dim I As Long
For I = 0 To Highest - Lowest
Result(I) = Lowest + I
Next
FromRange = Result
End Function
答案 1 :(得分:1)
既然你已经标记了VB.NET,这应该可以转换为VB6(我已经避免了Linq):
Public Shared Function FillGaps(input As Int32()) As Int32()
Dim output = New List(Of Int32)
Array.Sort(input)
' now we'll find the min/max-values at the first/last indices
For i As Int32 = input(0) To input(input.Length - 1)
output.Add(i)
Next
Return output.ToArray()
End Function
以下列方式使用它:
Dim intArray = {19, 17, 20, 16}
intArray = FillGaps(intArray)
请注意,此方法会跳过重复项。