vb使用折半查找法,在一批有序数列中查找给定的数x。程序已写好,帮忙检查下,这里个程序哪出错了?
帮忙检查下,这里个程序哪出错了?写出正确的全部源码。谢谢了,只有23分了,只能设置20分全部送给回答正确的人。谢谢了。。。使用折半查找法,在一批有序数列中查找给定的数x。...
帮忙检查下,这里个程序哪出错了?写出正确的全部源码。谢谢了,只有23分了,只能设置20分 全部送给回答正确的人。谢谢了。。。
使用折半查找法,在一批有序数列中查找给定的数x。
编程分析:设n个有序数(从小到大)存放在数组a(1)----a(n)中,要查找的数为x。用变量bot、top、mid 分别表示查找数据范围的底部(数组下界)、顶部(数组的上界)和中间,mid=(top+bot)/2,折半查找的算法如下:
(1)x=a(mid),则已找到退出循环,否则进行下面的判断;
(2)x<a(mid),x必定落在bot和mid-1的范围之内,即top=mid-1;
(3)x>a(mid),x必定落在mid+1和top的范围之内,即bot=mid+1;
(4)在确定了新的查找范围后,重复进行以上比较,直到找到或者bot<=top。
将上面的算法写成如下函数,若找到则返回该数所在的下标值,没找到则返回-1.
程序如下:
Function search(a() As Integer, x As Integer) As Integer
Dim bot%, top%, mid%
Dim find As Boolean '代表是否找到
bot = LBound(a)
top = UBound(a)
find = False '判断是否找到的逻辑变量,初值为False
Do While bot <= top And Not find
mid = (top + bot) \ 2
If x = a(mid) Then
find = True
Exit Do
ElseIf x < a(mid) Then
top = mid - 1
Else
bot = mid + 1
End If
Loop
If find Then
search = mid
Else
search = -1
End If
End Function
Private Sub Form_Click()
Dim i As Integer, x() As Integer
Dim y As Integer, k As Integer
x = Array(1, 4, 8, 10, 20, 30, 40, 46, 50, 55, 60, 64)
For i = LBound(x) To UBound(x) '打印输出数组x中的数据
Print x(i);
Next i
Print
y = Val(InputBox("输入要查找的数"))
k = search(x, y) ' 调用查找函数查找数据y
If k = -1 Then
Print "没有找到"; y
Else
Print "找到了,它是第" & k & "个数据"
End If
End Sub
帮忙检查下,这里个程序哪出错了?写出正确的全部源码。谢谢了,只有23分了,只能设置20分 全部送给回答正确的人。谢谢了。。。 展开
使用折半查找法,在一批有序数列中查找给定的数x。
编程分析:设n个有序数(从小到大)存放在数组a(1)----a(n)中,要查找的数为x。用变量bot、top、mid 分别表示查找数据范围的底部(数组下界)、顶部(数组的上界)和中间,mid=(top+bot)/2,折半查找的算法如下:
(1)x=a(mid),则已找到退出循环,否则进行下面的判断;
(2)x<a(mid),x必定落在bot和mid-1的范围之内,即top=mid-1;
(3)x>a(mid),x必定落在mid+1和top的范围之内,即bot=mid+1;
(4)在确定了新的查找范围后,重复进行以上比较,直到找到或者bot<=top。
将上面的算法写成如下函数,若找到则返回该数所在的下标值,没找到则返回-1.
程序如下:
Function search(a() As Integer, x As Integer) As Integer
Dim bot%, top%, mid%
Dim find As Boolean '代表是否找到
bot = LBound(a)
top = UBound(a)
find = False '判断是否找到的逻辑变量,初值为False
Do While bot <= top And Not find
mid = (top + bot) \ 2
If x = a(mid) Then
find = True
Exit Do
ElseIf x < a(mid) Then
top = mid - 1
Else
bot = mid + 1
End If
Loop
If find Then
search = mid
Else
search = -1
End If
End Function
Private Sub Form_Click()
Dim i As Integer, x() As Integer
Dim y As Integer, k As Integer
x = Array(1, 4, 8, 10, 20, 30, 40, 46, 50, 55, 60, 64)
For i = LBound(x) To UBound(x) '打印输出数组x中的数据
Print x(i);
Next i
y = Val(InputBox("输入要查找的数"))
k = search(x, y) ' 调用查找函数查找数据y
If k = -1 Then
Print "没有找到"; y
Else
Print "找到了,它是第" & k & "个数据"
End If
End Sub
帮忙检查下,这里个程序哪出错了?写出正确的全部源码。谢谢了,只有23分了,只能设置20分 全部送给回答正确的人。谢谢了。。。 展开
展开全部
Option Base 1
Function search(a() As Variant, x As Integer) As Integer
Dim bot%, top%, mid%
Dim find As Boolean '代表是否找到
bot = LBound(a)
top = UBound(a)
find = False '判断是否找到的逻辑变量,初值为False
Do While bot <= top And Not find
mid = (top + bot) \ 2
If x = a(mid) Then
find = True
Exit Do
ElseIf x < a(mid) Then
top = mid - 1
Else
bot = mid + 1
End If
Loop
If find Then
search = mid
Else
search = -1
End If
End Function
Private Sub Form_Click()
Dim i As Integer, x() As Variant
Dim y As Integer, k As Integer
x = Array(1, 4, 8, 10, 20, 30, 40, 46, 50, 55, 60, 64)
For i = LBound(x) To UBound(x) '打印输出数组x中的数据
Print x(i);
Next i
Print
y = Val(InputBox("输入要查找的数"))
k = search(x, y) ' 调用查找函数查找数据y
If k = -1 Then
Print "没有找到"; y
Else
Print "找到了,它是第" & k & "个数据"
End If
End Sub
Function search(a() As Variant, x As Integer) As Integer
Dim bot%, top%, mid%
Dim find As Boolean '代表是否找到
bot = LBound(a)
top = UBound(a)
find = False '判断是否找到的逻辑变量,初值为False
Do While bot <= top And Not find
mid = (top + bot) \ 2
If x = a(mid) Then
find = True
Exit Do
ElseIf x < a(mid) Then
top = mid - 1
Else
bot = mid + 1
End If
Loop
If find Then
search = mid
Else
search = -1
End If
End Function
Private Sub Form_Click()
Dim i As Integer, x() As Variant
Dim y As Integer, k As Integer
x = Array(1, 4, 8, 10, 20, 30, 40, 46, 50, 55, 60, 64)
For i = LBound(x) To UBound(x) '打印输出数组x中的数据
Print x(i);
Next i
y = Val(InputBox("输入要查找的数"))
k = search(x, y) ' 调用查找函数查找数据y
If k = -1 Then
Print "没有找到"; y
Else
Print "找到了,它是第" & k & "个数据"
End If
End Sub
追问
这个Option Base 1 这个代码 可有可无吧?a() As Variant 问题啊 唉 、、、
顺便,再问下:)本例中的Form1_Click()事件中能否改为k=Find(x,y)?
谢谢
追答
Array 函数返回的是 变体型数据 所以你的x数组变量类型要更改下
其二 数组默认下标是从0开始的 如果你没有 Option Base 1 那么如果找到的数据在第二个元素 却显示 1 那不就不对了么
k = search(x, y) 改为 k=find(x,y) 当然可以 只要你对应的
Function search(a() As Variant, x As Integer) As Integer
也改为
Function find(a() As Variant, x As Integer) As Integer
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询