|
| 1 | +' _____________________________________ |
| 2 | +' https://github.com/kenysdev |
| 3 | +' 2024 - vb.net |
| 4 | +' _____________________________________ |
| 5 | +' 35 REPARTIENDO LOS ANILLOS DE PODER |
| 6 | +' ------------------------------------ |
| 7 | +' ¡La temporada 2 de "Los Anillos de Poder" está a punto de estrenarse! |
| 8 | +' ¿Qué pasaría si tuvieras que encargarte de repartir los anillos |
| 9 | +' entre las razas de la Tierra Media? |
| 10 | +' Desarrolla un programa que se encargue de distribuirlos. |
| 11 | +' Requisitos: |
| 12 | +' 1. Los Elfos recibirán un número impar. |
| 13 | +' 2. Los Enanos un número primo. |
| 14 | +' 3. Los Hombres un número par. |
| 15 | +' 4. Sauron siempre uno. |
| 16 | +' Acciones: |
| 17 | +' 1. Crea un programa que reciba el número total de anillos |
| 18 | +' y busque una posible combinación para repartirlos. |
| 19 | +' 2. Muestra el reparto final o el error al realizarlo. |
| 20 | + |
| 21 | +Module Program |
| 22 | + Function GetTotalRings() As Integer |
| 23 | + Do |
| 24 | + Console.Write("Cantidad de anillos: ") |
| 25 | + Dim input = Console.ReadLine() |
| 26 | + Dim result As Integer |
| 27 | + If Integer.TryParse(input, result) AndAlso result >= 1 Then |
| 28 | + Return result |
| 29 | + End If |
| 30 | + Console.WriteLine("Debe ser un valor entero '>= 1'.") |
| 31 | + Loop |
| 32 | + End Function |
| 33 | + |
| 34 | + Function IsPrime(n As Integer) As Boolean |
| 35 | + If n < 2 Then Return False |
| 36 | + For i As Integer = 2 To Math.Sqrt(n) |
| 37 | + If n Mod i = 0 Then Return False |
| 38 | + Next |
| 39 | + Return True |
| 40 | + End Function |
| 41 | + |
| 42 | + Function Distribute(total As Integer) As List(Of (Integer, Integer, Integer)) |
| 43 | + Dim combinations As New List(Of (Integer, Integer, Integer)) |
| 44 | + Dim range As Integer |
| 45 | + Dim start As Integer |
| 46 | + Dim [end] As Integer |
| 47 | + |
| 48 | + If total > 1000 Then |
| 49 | + ' For large numbers. |
| 50 | + range = Math.Min(1000, total \ 3) |
| 51 | + start = (total - range) \ 2 |
| 52 | + [end] = start + range |
| 53 | + Else |
| 54 | + ' For smaller numbers. |
| 55 | + start = 1 |
| 56 | + [end] = total - 1 |
| 57 | + End If |
| 58 | + |
| 59 | + For elves As Integer = start To [end] Step 2 |
| 60 | + For men As Integer = start + 1 To [end] Step 2 |
| 61 | + Dim dwarves As Integer = total - (men + elves) |
| 62 | + If dwarves > 0 AndAlso IsPrime(dwarves) Then |
| 63 | + combinations.Add((elves, men, dwarves)) |
| 64 | + End If |
| 65 | + Next |
| 66 | + Next |
| 67 | + |
| 68 | + Return combinations |
| 69 | + End Function |
| 70 | + |
| 71 | + Function StandardDeviation(tup As (Integer, Integer, Integer)) As Double |
| 72 | + Dim values() As Double = {tup.Item1, tup.Item2, tup.Item3} |
| 73 | + Dim avg As Double = values.Average() |
| 74 | + Dim sum As Double = values.Sum(Function(d) Math.Pow(d - avg, 2)) |
| 75 | + Return Math.Sqrt(sum / (values.Length - 1)) |
| 76 | + End Function |
| 77 | + |
| 78 | + Function TheMostBalanced(combinations As List(Of (Integer, Integer, Integer))) As (Integer, Integer, Integer) |
| 79 | + Return combinations.OrderBy(Function(c) StandardDeviation(c)).First() |
| 80 | + End Function |
| 81 | + |
| 82 | + Sub PrintResult(distribution As (Integer, Integer, Integer), sauron As Integer) |
| 83 | + If distribution.Equals(Nothing) Then |
| 84 | + Console.WriteLine("Error en la selección equitativa.") |
| 85 | + Return |
| 86 | + End If |
| 87 | + |
| 88 | + Console.WriteLine("_________________________") |
| 89 | + Console.WriteLine($"Elfos -> {distribution.Item1} : # Impar") |
| 90 | + Console.WriteLine($"Enanos -> {distribution.Item3} : # Primo") |
| 91 | + Console.WriteLine($"Hombres -> {distribution.Item2} : # Par") |
| 92 | + Console.WriteLine($"Sauron -> {sauron} : # Fijo") |
| 93 | + Console.WriteLine("-------------------------") |
| 94 | + End Sub |
| 95 | + |
| 96 | + Sub Main() |
| 97 | + Console.WriteLine("REPARTIENDO LOS ANILLOS DE PODER") |
| 98 | + Dim total As Integer = GetTotalRings() |
| 99 | + Dim sauron As Integer = 1 |
| 100 | + total -= sauron |
| 101 | + |
| 102 | + Dim combinations = Distribute(total) |
| 103 | + If combinations.Count = 0 Then |
| 104 | + Console.WriteLine("No existe una combinación posible.") |
| 105 | + Return |
| 106 | + End If |
| 107 | + |
| 108 | + Dim distribution = TheMostBalanced(combinations) |
| 109 | + PrintResult(distribution, sauron) |
| 110 | + End Sub |
| 111 | +End Module |
0 commit comments