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