如何找一个二维数组的鞍点?
找鞍点:在一个矩阵中,如果i行上的最大值a(i,j)同时是第j列上最小值,则称该数是一个鞍点。例如:
1 4 9 8
3 4 6 1
7 5 8 6
在上面的矩阵中,第二行第三列的元素6就是这个矩阵的鞍点。
下面是我写的程序,经过测试,漏洞百出。请大家指点迷津
Private Sub Command1_Click()
Const M As Integer = 3
Const N As Integer = 3
Dim min As Integer
Dim max As Integer
Dim p As Integer
Dim q As Integer
Dim data(M, N) As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Randomize Timer
For i = 1 To M
For j = 1 To N
data(i, j) = Rnd * 50
Next
Next
For i = 1 To M
For j = 1 To N
Print data(i, j),
Next
Next
For i = 1 To M
For j = 1 To N
If data(i, j) > max Then
p = i: q = j
max = data(i, j)
End If
Next
min = max
For k = 1 To M
If min >= data(k, q) Then
min = data(k, q)
Exit For
End If
Next
If max = min Then
Print "找到鞍点:"; max
Print "其行值为:"; p, "其列值为:"; q
Exit Sub
End If
Next
Print "没找到鞍点"
End Sub