主题:请问这个咋做啊
lushui
[专家分:0] 发布于 2009-11-11 19:50:00
有一个n*m的矩阵,编写程序,找出其中最大的那个元素所在的行和列,并输出其值及行号和列号
回复列表 (共1个回复)
沙发
一江秋水 [专家分:9680] 发布于 2009-11-12 09:06:00
下面的代码中,如果有2个以上相同的最大数,那么只以最先出现的为准
Private Sub Command1_Click()
Dim mn() As Integer, max As Integer
Dim m As Integer, n As Integer, w As Integer, h As Integer, i As Integer, j As Integer
m = 4: n = 5
ReDim mn(1 To m, 1 To n)
Randomize
For i = 1 To m
For j = 1 To n
mn(i, j) = Rnd * 100 '创建100以内的随机数
Debug.Print mn(i, j); " ";
Next
Debug.Print
Next
For i = 1 To m
For j = 1 To n
If mn(i, j) > max Then w = i: h = j: max = mn(i, j)
Next
Next
Debug.Print "最大数:" & max & " 位于:" & w; "行" & h & "列"
End Sub
我来回复