Menu
Icon

Granjero obsesivo asp.

{

En este ejemplo de vb.net aremos la construcción del ejercicio del granjero obsesivo,

Ejercicio sacado del libro de océano el cual dice:

Un granjero tiene 36 gallinas. Por alguna razón, ha construido un gallinero con las jaulas distribuidas

Como muestra la figura:


Coloca en cada jaula una cantidad diferente de gallina, pudiendo haber desde 1 hasta 8 gallinas, sin repetir la cantidad en otra jaula, teniendo tanta obsesión el granjero  que además de eso dos jaulas vecinas (no continuas por un lado ni en diagonal) no pueden contener cantidades consecutivas de gallina. Es decir, si una jaula contiene por ejemplo 5 gallinas, en las vecinas no puede haber ni 4 ni 6,  además de que en cualquier otra jaula no podrá haber 5 gallinas.

 ¿De qué modo distribuye el granjero sus gallinas?


Para hacer este ejercicio debemos crear el diseño el cual es el ya mostrado:

El cual consta de 8 textboxt que representarían las jaulas donde pondremos x cantidad de gallinas
Un button que nos dirá si repartimos correctamente las gallinas.

Leyendo el ejercicio se deben reconocer las condiciones como ser:

  1.         I.            Números del 1 al 8

  2.       II.            Todas las jaulas con una cantidad diferente o que las mismas sumen 36

  3.     III.            No permitir cantidades consecutivas por jaulas vecinas

 

Definiéndolas solo faltaría editar el código

aqui el codigo


Public Class appletenhtml
    Private Sub ok_Click(sender As Object, e As EventArgs) Handles ok.Click
    'numeros del 1 al 8   
If (CInt(TextBox1.Text) > 0 And CInt(TextBox1.Text) <= 8 And CInt(TextBox2.Text) > 0 And CInt(TextBox2.Text) <= 8 And CInt(TextBox3.Text) > 0 And CInt(TextBox3.Text) <= 8 And CInt(TextBox4.Text) > 0 And CInt(TextBox4.Text) <= 8 And CInt(TextBox5.Text) > 0 And CInt(TextBox5.Text) <= 8 And CInt(TextBox6.Text) > 0 And CInt(TextBox6.Text) <= 8 And CInt(TextBox7.Text) > 0 And CInt(TextBox7.Text) <= 8 And CInt(TextBox8.Text) > 0 And CInt(TextBox8.Text) <= 8) Then
            If ((CInt(TextBox1.Text) + CInt(TextBox2.Text) + CInt(TextBox3.Text) + CInt(TextBox4.Text) + CInt(TextBox5.Text) + CInt(TextBox6.Text) + CInt(TextBox7.Text) + CInt(TextBox8.Text)) = 36) Then
             'sumen 36
                ''------------------- no permitir cantidades consecutivas por jaulas vecinas
                If ((CInt(TextBox1.Text) + 1) <> CInt(TextBox2.Text) And (CInt(TextBox1.Text) - 1) <> CInt(TextBox2.Text)) Then
                    If ((CInt(TextBox1.Text) + 1) <> CInt(TextBox4.Text) And (CInt(TextBox1.Text) - 1) <> CInt(TextBox4.Text)) Then
                        If ((CInt(TextBox2.Text) + 1) <> CInt(TextBox5.Text) And (CInt(TextBox2.Text) - 1) <> CInt(TextBox5.Text)) Then
                            If ((CInt(TextBox4.Text) + 1) <> CInt(TextBox5.Text) And (CInt(TextBox4.Text) - 1) <> CInt(TextBox5.Text)) Then
                                If ((CInt(TextBox3.Text) + 1) <> CInt(TextBox4.Text) And (CInt(TextBox3.Text) - 1) <> CInt(TextBox4.Text)) Then
                                    If ((CInt(TextBox5.Text) + 1) <> CInt(TextBox6.Text) And (CInt(TextBox5.Text) - 1) <> CInt(TextBox6.Text)) Then
                                        If ((CInt(TextBox4.Text) + 1) <> CInt(TextBox7.Text) And (CInt(TextBox4.Text) - 1) <> CInt(TextBox7.Text)) Then
                                            If ((CInt(TextBox5.Text) + 1) <> CInt(TextBox8.Text) And (CInt(TextBox5.Text) - 1) <> CInt(TextBox8.Text)) Then
                                                If ((CInt(TextBox7.Text) + 1) <> CInt(TextBox8.Text) And (CInt(TextBox7.Text) - 1) <> CInt(TextBox8.Text)) Then
                                                    MsgBox("Correcto", MsgBoxStyle.MsgBoxRight)
                                                End If
                      End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
                ''--------------------------
            Else
                MsgBox("No repetir los numeros")
            End If
        Else
            MsgBox("error numeros del 1 al 8")
        End If
    End Sub
End Class


Saludos.

Descargar ejemplo:

}

Utilizamos cookies propias y de terceros con finalidades analiticas y para mejorar tu experiencia de compras segun tus preferencias, mediante el analisis de tus habitos de navegacion y tu perfil. Puedes aceptar todas las cookies pulsando el boton "Aceptar" o gestionar tus preferencias en el panel de configuracion