1

自作のアルゴリズムを使った二分探索が必要です。

これでプログラムは機能するはずですが、残念ながらそうではありません。彼はそれを比較しましたが、問題は都市に課金していることです。見つからない場合は出力に表示されます。

この演習の目的は、検索値を郵便番号と比較することです。郵便番号には、郵便番号と都市の名前が含まれています。

Module Module1

    Sub Main()
        '                           0       1          2       3            4       5         6       7         8       9     
        Dim zipcodes() As String = {"1000", "Brussel", "2000", "Antwerpen", "3000", "Leuven", "8000", "Brugge", "9000", "Gent"}
        'Dim zipcodesCount As Integer = 5
        '
        Dim searchValues() As String = {"0500", "1000", "2000", "3000", "4000", "8000", "9000", "9500"}
        ' 
        Dim searchValueIndex As Integer
        For searchValueIndex = 0 To 7
            Dim searchValue As String = searchValues(searchValueIndex)
            '
            Dim index As Integer
            Dim found As Boolean = False
            Dim allesDoorzocht As Boolean = False

            Dim uBound, lBound As Integer
            uBound = zipcodes.GetUpperBound(0) - 1
            lBound = zipcodes.GetLowerBound(0)


            Do Until found OrElse allesDoorzocht

                index = (lBound + uBound + 1 ) \ 2

                If (searchValue = zipcodes(index)) Then
                    found = True
                End If

                If uBound <= lBound Then
                    allesDoorzocht = True
                End If

                If Not (found OrElse allesDoorzocht) Then

                    If searchValue > zipcodes(index) Then
                        lBound = index + 1
                    Else
                        uBound = index - 1
                    End If

                End If

            Loop


            If found Then
                Console.WriteLine(searchValue & " is zipcode of " & zipcodes(index + 1))
                ' Of : Console.WriteLine(searchValue & " is zipcode of " & zipcodes(index + 1))
            Else
                Console.WriteLine(searchValue & " not found")
            End If
        Next
        '
        Console.ReadLine()
    End Sub
End Module
4

2 に答える 2

1

City オブジェクトを作成する

Public Class City
    Public Property ZipCode As String 
    Public Property Name As String 
End Class

City代わりに配列を操作します

Dim citiy() As City = { _
    New City With { .ZipCode = "1000", .Name = "Brussel" }, _
    New City With { .ZipCode = "2000", .Name = "Antwerpen" }, _
    New City With { .ZipCode = "3000", .Name = "Leuven" }, _
    New City With { .ZipCode = "8000", .Name = "Brugge" }, _
    New City With { .ZipCode = "9000", .Name = "Gent" } _
}

これで、郵便番号と名前に次のようにアクセスできます

city(i).ZipCode
city(i).Name

対応する都市名と郵便番号のインデックスは同じです。


VS 2010 で導入された自動プロパティとオブジェクト初期化子を使用していることに注意してください。行継続記号 _. (私はまだ VS 2008 を使用しており、通常は C# を使用しています)

于 2012-11-17T22:07:10.380 に答える
0

あなたが書いた二分探索のために昇順でなければならない配列郵便番号を検索していますが、そうで"1000"<="Brussel"<="2000"はありません。アルゴリズムが配列の偶数要素のみを考慮していることを確認する必要があります。あなたは分割によってそれを完璧に行いました。しかし、見つからない場合は、境界が都市名ではなく偶数のインデックスを指していることを確認する必要があります。したがって、lBound=index+2またはする必要がありますuBound=index-2

于 2012-11-17T21:43:38.193 に答える