我有二维整数array x, y
,如下所示
{1160, 0},
{1560, 400},
{11940, 10380},
{12480, 540},
{12540, 60},
{12600, 60},
{12720, 120},
{13120, 400},
{13380, 260},
{13680, 300},
{14000, 320}
我需要选择{13120, 400}
。我通过按降序排列x
值来获取此值,并获取y
值从前一记录增加的第一对。
答案 0 :(得分:0)
试试这个:
Function GetLargestPair(ByVal data(,) As Integer) As Integer()
'First convert to data structure that's easier to use
Dim records As New List(Of Integer())
For i As Integer = 0 To data.GetUpperBound(0) - 1
records.Add(New Integer() {data(i, 0), data(i, 1)})
Next i
'Now it's very easy to sort descending by the X values:
Dim sorted = records.OrderByDescending(Function(r) r(0)).ThenBy(Function(r) r(1))
'And then use that to find the result
Dim PrevY As Integer = Integer.MaxValue
For Each pair As Integer() In sorted
If pair(1) > PrevY Then Return pair
PrevY = pair(1)
Next
'Not clear what you want in this case. I just returned the overall largest X,
' but you might prefer Nothing or throwing an exception
Return sorted.First()
End Function
这样称呼:
'Sample data
Dim data = {{1160, 0}, {1560, 400}, {11940, 10380}, {12480, 540}, {12540, 60}, {12600, 60}, {12720, 120}, {13120, 400}, {13380, 260}, {13680, 300}, {14000, 320}}
Dim result = GetLargestPair(data)
'result should now contain {13120, 400}