excel 每列中最小值的最大值

hfyxw5xn  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(202)

根据给定的示例,存在从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
3pvhb19x

3pvhb19x1#

获取列最小值的最大值

  • 分钟的计算是错误的,罗恩罗森菲尔德也分享了你的意见。
  • 我使用了这个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的结果

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

相关问题