现在的位置: 首页 > 综合 > 正文

爱因斯坦的智力题

2013年10月14日 ⁄ 综合 ⁄ 共 5886字 ⁄ 字号 评论关闭

(1)New WindowsApplication;

(2)Form1上拖拽Lable1、Lable2。 

Public Class Form1

    Dim data As New ArrayList

    Public Enum color
        red
        yellow
        green
        blue
        white
    End Enum

    Public Enum country
        England
        Sweden
        Denmark
        Norway
        German
    End Enum

    Public Enum drink
        tea
        coffee
        milk
        water
        beer
    End Enum

    Public Enum smoke
        Pall_Mall
        Dunhill
        Blends
        Blue_Master
        Prince
    End Enum

    Public Enum position
        A
        B
        C
        D
        E
    End Enum

    Public Enum pet
        dog
        bird
        cat
        horse
        fish
    End Enum

    Public Structure person
        Dim col As color
        Dim cou As country
        Dim dri As drink
        Dim smo As smoke
        Dim pos As position
        Dim pe As pet
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Label1.Text = "1、在一条街上,有5座房子,喷了5种颜色。" & vbCrLf & _
        "2、每个房里住着不同国籍的人。" & vbCrLf & _
        "3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。问题是:谁养鱼?" & vbCrLf & vbCrLf & _
  "1、英国人住红色房子" & vbCrLf & _
  "2、瑞典人养狗" & vbCrLf & _
  "3、丹麦人喝茶" & vbCrLf & _
  "4、绿色房子在白色房子左面" & vbCrLf & _
  "5、绿色房子主人喝咖啡" & vbCrLf & _
  "6、抽Pall Mall 香烟的人养鸟" & vbCrLf & _
  "7、黄色房子主人抽Dunhill香烟" & vbCrLf & _
  "8、住在中间房子的人喝牛奶" & vbCrLf & _
  "9、挪威人住第一间房" & vbCrLf & _
  "10、抽Blends香烟的人住在养猫的人隔壁" & vbCrLf & _
  "11、养马的人住抽Dunhill香烟的人隔壁" & vbCrLf & _
  "12、抽Blue Master的人喝啤酒  " & vbCrLf & _
  "13、德国人抽Prince香烟" & vbCrLf & _
  "14、挪威人住蓝色房子隔壁" & vbCrLf & _
  "15、抽Blends香烟的人有一个喝水的邻居"

        Dim per(4) As person
        Dim tt As Integer() = {0, 1, 2, 3, 4}
        pai(tt, 0, 5)

        For intColor As Integer = 0 To data.Count - 1
            For i As Integer = 0 To 4
                per(i).col = CType(CType(data(intColor), Integer())(i), color)
            Next
            For intCountry As Integer = 0 To data.Count - 1
                For i As Integer = 0 To 4
                    per(i).cou = CType(CType(data(intCountry), Integer())(i), country)
                Next
                ''''''''   
                If Not per(getIndex(per, "country", country.England)).col = color.red Then
                    GoTo country_end
                End If
                ''''''''   
                For intDrink As Integer = 0 To data.Count - 1
                    For i As Integer = 0 To 4
                        per(i).dri = CType(CType(data(intDrink), Integer())(i), drink)
                    Next
                    '''''''''   
                    If Not per(getIndex(per, "country", country.Denmark)).dri = drink.tea Then
                        GoTo drink_end
                    End If

                    If Not per(getIndex(per, "color", color.green)).dri = drink.coffee Then
                        GoTo drink_end
                    End If
                    '''''''''   
                    For intSmoke As Integer = 0 To data.Count - 1
                        For i As Integer = 0 To 4
                            per(i).smo = CType(CType(data(intSmoke), Integer())(i), smoke)
                        Next
                        ''''''   
                        If Not per(getIndex(per, "color", color.yellow)).smo = smoke.Dunhill Then
                            GoTo smoke_end
                        End If

                        If Not per(getIndex(per, "smoke", smoke.Blue_Master)).dri = drink.beer Then
                            GoTo smoke_end
                        End If

                        If Not per(getIndex(per, "smoke", smoke.Prince)).cou = country.German Then
                            GoTo smoke_end
                        End If
                        ''''''   
                        For intPet As Integer = 0 To data.Count - 1
                            For i As Integer = 0 To 4
                                per(i).pe = CType(CType(data(intPet), Integer())(i), pet)
                            Next

                            '''''   
                            If Not per(getIndex(per, "country", country.Sweden)).pe = pet.dog Then
                                GoTo pet_end
                            End If

                            If Not per(getIndex(per, "smoke", smoke.Pall_Mall)).pe = pet.bird Then
                                GoTo pet_end
                            End If
                            '''''   
                            For intPos As Integer = 0 To data.Count - 1
                                For i As Integer = 0 To 4
                                    per(i).pos = CType(CType(data(intPos), Integer())(i), position)
                                Next
                                '''''   
                                If Not per(getIndex(per, "position", position.C)).dri = drink.milk Then
                                    GoTo pos_end
                                End If

                                If Not per(getIndex(per, "position", position.A)).cou = country.Norway Then
                                    GoTo pos_end
                                End If
                                '''''   
                                If Check(per) Then
                                    Dim result As String = ""
                                    For i As Integer = 0 To 4
                                        result &= per(i).col.ToString & "|" & per(i).cou.ToString & "|" & per(i).dri.ToString & "|" & per(i).pe.ToString & "|" & per(i).pos.ToString & "|" & per(i).smo.ToString & vbCrLf & vbNewLine
                                    Next

                                    Me.Label2.Text = result
                                    Exit Sub
                                End If
pos_end:
                            Next
pet_end:
                        Next
smoke_end:
                    Next
drink_end:
                Next
country_end:
            Next
        Next
    End Sub

    Private Function Check(ByVal per() As person) As Boolean
        Dim result As Integer = 0
        'conditon   1   
        If per(getIndex(per, "country", country.England)).col = color.red Then
            result += 1
        End If
        'conditon   2   
        If per(getIndex(per, "country", country.Sweden)).pe = pet.dog Then
            result += 1
        End If
        'conditon   3   
        If per(getIndex(per, "country", country.Denmark)).dri = drink.tea Then
            result += 1
        End If

        'conditon   4   
        Dim pos_green, pos_white As position
        pos_green = per(getIndex(per, "color", color.green)).pos
        pos_white = per(getIndex(per, "color", color.white)).pos
        If pos_green + 1 = pos_white Then
            result += 1
        End If

        'conditon   5   
        If per(getIndex(per, "color", color.green)).dri = drink.coffee Then
            result += 1
        End If

        'conditon   6   
        If per(getIndex(per, "smoke", smoke.Pall_Mall)).pe = pet.bird Then
            result += 1
        End If

        'conditon   7   
        If per(getIndex(per, "color", color.yellow)).smo = smoke.Dunhill Then
            result += 1
        End If

        'conditon   8   
        If per(getIndex(per, "position", position.C)).dri = drink.milk Then
            result += 1
        End If

        'conditon   9   
        If per(getIndex(per, "country", country.Norway)).pos = position.A Then
            result += 1
        End If

        'conditon   10   
        Dim pos_blend, pos_cat As position
        pos_blend = per(getIndex(per, "smoke", smoke.Blends)).pos
        pos_cat = per(getIndex(per, "pet", pet.cat)).pos
        If pos_blend + 1 = pos_cat Or pos_blend - 1 = pos_cat Then
            result += 1
        End If

        'conditon   11   
        Dim pos_dunhill, pos_horse As position
        pos_dunhill = per(getIndex(per, "smoke", smoke.Dunhill)).pos
        pos_horse = per(getIndex(per, "pet", pet.horse)).pos
        If pos_dunhill + 1 = pos_horse Or pos_dunhill - 1 = pos_horse Then
            result += 1
        End If

        'conditon   12   
        If per(getIndex(per, "smoke", smoke.Blue_Master)).dri = drink.beer Then
            result += 1
        End If

        'conditon   13   
        If per(getIndex(per, "country", country.German)).smo = smoke.Prince Then
            result += 1
        End If

        'conditon   14   
        Dim pos_nowei, pos_blue As position
        pos_nowei = per(getIndex(per, "country", country.Norway)).pos
        pos_blue = per(getIndex(per, "color", color.blue)).pos
        If pos_nowei + 1 = pos_blue Or pos_nowei - 1 = pos_blue Then
            result += 1
        End If

        'conditon   15   
        Dim pos_blends, pos_water As position
        pos_blends = per(getIndex(per, "smoke", smoke.Blends)).pos
        pos_water = per(getIndex(per, "drink", drink.water)).pos
        If pos_blends + 1 = pos_water Or pos_blends - 1 = pos_water Then
            result += 1
        End If

        If result = 15 Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub pai(ByRef a As Integer(), ByVal m As Integer, ByVal n As Integer)
        Dim k As Integer
        If m < n Then
            For k = 0 To m
                pai(a, m + 1, n)
                change(a, m)
            Next
        Else
            data.Add(a.Clone)
        End If
    End Sub

    Private Sub change(ByRef a As Integer(), ByVal m As Integer)
        Dim i As Integer
        Dim temp As Integer = a(0)
        For i = 0 To m - 1
            a(i) = a(i + 1)
        Next
        a(i) = temp
    End Sub

    Private Function getIndex(ByVal per() As person, ByVal col As String, ByVal value As Integer) As Integer
        Select Case col
            Case "color"
                For i As Integer = 0 To 4
                    If per(i).col = value Then
                        Return i
                    End If
                Next
            Case "country"
                For i As Integer = 0 To 4
                    If per(i).cou = value Then
                        Return i
                    End If
                Next
            Case "drink"
                For i As Integer = 0 To 4
                    If per(i).dri = value Then
                        Return i
                    End If
                Next
            Case "pet"
                For i As Integer = 0 To 4
                    If per(i).pe = value Then
                        Return i
                    End If
                Next
            Case "smoke"
                For i As Integer = 0 To 4
                    If per(i).smo = value Then
                        Return i
                    End If
                Next
            Case "position"
                For i As Integer = 0 To 4
                    If per(i).pos = value Then
                        Return i
                    End If
                Next
        End Select
    End Function
End Class

抱歉!评论已关闭.