根據給定的示例,有一個從 n 到 n 的陣列
aij = 12 – 3i j
i - 列號 j- 行號,需要找到每列中最小值的最大值,我撰寫了代碼,但確實如此不輸出它應該的,請幫助
Sub task1()
Dim i, j, m As Integer
Dim a(5, 5) As Integer
Dim max(5) As Integer
For i = 0 To 4
For j = 0 To 4
a(i, j) = 12 - 3 * i - j
Next j
Next i
For i = 0 To 4
max(i) = a(i, 0)
For j = 1 To 4
If a(i, j) < max(i) Then max(i) = a(i, j)
Next j
Next i
m = max(0)
For i = 1 To 4
If max(i) > m Then m = max(i)
Next i
MsgBox m
End Sub
uj5u.com熱心網友回復:
獲取列最小值的最大值
- Ron Rosenfeld 在你的評論中也分享了分鐘的計算是錯誤的。
- 我使用了這個
aij = 12 – 3i j導致2 * (6 - n). - 如果是
- j,就像在您的代碼中一樣,只需在此代碼中更改它。那么結果將是3 * (4 - n)。
保持關閉
Sub Task1()
Const n As Integer = 4
If n < 0 Then Exit Sub
Dim a(0 To n, 0 To n) As Integer
Dim mins(0 To n) As Integer
Dim i As Integer, j As Integer, max As Integer
' Populate the array.
For i = 0 To n
For j = 0 To n
a(i, j) = 12 - 3 * i j
Next j
Next i
PrintData a, , " | ", "The Array" '***
' Get the column minimums.
For j = 0 To n
mins(j) = a(0, j)
For i = 1 To n
If a(i, j) < mins(j) Then mins(j) = a(i, j)
Next i
Debug.Print "The minimum of column " & j & " is " & mins(j) & "." '***
Next j
' Get the maximum of the column minimums.
max = mins(0)
For i = 1 To n
If mins(i) > max Then max = mins(i)
Next i
Debug.Print "The maximum of the column minimums is " & max & "." '***
' MsgBox max
End Sub
n = 4 時的 Debug.Print 結果
The Array
12 | 13 | 14 | 15 | 16
9 | 10 | 11 | 12 | 13
6 | 7 | 8 | 9 | 10
3 | 4 | 5 | 6 | 7
0 | 1 | 2 | 3 | 4
The minimum of column 0 is 0.
The minimum of column 1 is 1.
The minimum of column 2 is 2.
The minimum of column 3 is 3.
The minimum of column 4 is 4.
The maximum of the column minimums is 4.
結果
2 * (6 - n)
n | 0 1 2 3 4 5 6 7 8 9
max | 12 10 8 6 4 2 0 -2 -4 -6
擺脫 Mins 陣列
Sub GetMinsMax()
Const n As Integer = 4
If n < 0 Then Exit Sub
Dim a(0 To n, 0 To n) As Integer
Dim i As Integer, j As Integer, min As Integer, max As Integer
For j = 0 To n
For i = 0 To n
' Populate the current element.
a(i, j) = 12 - 3 * i j
' Update the column minimum.
If i = 0 Then
min = a(0, j)
Else
If a(i, j) < min Then min = a(i, j)
End If
Next i
Debug.Print "The minimum of column " & j & " is " & min & "." '***
' Update the maximum of the column minimums.
If j = 0 Then
max = min
Else
If min > max Then max = min
End If
Next j
PrintData a, , " | ", "The Array" '***
Debug.Print "The maximum of the column minimums is " & max & "." '***
' MsgBox max
End Sub
可視化
Sub PrintData( _
ByVal Data As Variant, _
Optional ByVal RowDelimiter As String = vbLf, _
Optional ByVal ColumnDelimiter As String = " ", _
Optional ByVal Title As String = "PrintData Result")
' Store the limits in variables
Dim rLo As Long: rLo = LBound(Data, 1)
Dim rHi As Long: rHi = UBound(Data, 1)
Dim cLo As Long: cLo = LBound(Data, 2)
Dim cHi As Long: cHi = UBound(Data, 2)
' Define the arrays.
Dim cLens() As Long: ReDim cLens(rLo To rHi)
Dim strData() As String: ReDim strData(rLo To rHi, cLo To cHi)
' For each column ('c'), store strings of the same length ('cLen')
' in the string array ('strData').
Dim r As Long, c As Long
Dim cLen As Long
For c = cLo To cHi
' Calculate the current column's maximum length ('cLen').
cLen = 0
For r = rLo To rHi
strData(r, c) = CStr(Data(r, c))
cLens(r) = Len(strData(r, c))
If cLens(r) > cLen Then cLen = cLens(r)
Next r
' Store strings of the same length in the current column
' of the string array.
If c = cHi Then ' last row (no column delimiter ('ColumnDelimiter'))
For r = rLo To rHi
strData(r, c) = Space(cLen - cLens(r)) & strData(r, c)
Next r
Else ' all but the last row
For r = rLo To rHi
strData(r, c) = Space(cLen - cLens(r)) & strData(r, c) _
& ColumnDelimiter
Next r
End If
Next c
' Write the title to the print string ('PrintString').
Dim PrintString As String: PrintString = Title
' Append the data from the string array to the print string.
For r = rLo To rHi
PrintString = PrintString & RowDelimiter
For c = cLo To cHi
PrintString = PrintString & strData(r, c)
Next c
Next r
' Print the print string.
Debug.Print PrintString
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/530992.html
標籤:擅长vba
上一篇:如何從用戶輸入框中選擇作業表
