Skip to content

Commit d8c4d53

Browse files
authored
Merge pull request mouredev#7151 from Hequebo/hequebo35
mouredev#35 - C#
2 parents 4f768c5 + bfb80de commit d8c4d53

File tree

1 file changed

+95
-0
lines changed
  • Roadmap/35 - REPARTIENDO LOS ANILLOS DE PODER/c#

1 file changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
class Distribution
2+
{
3+
private int _sauron;
4+
private int _elves;
5+
private int _dwarves;
6+
private int _men;
7+
8+
public int Sauron { get { return _sauron; } }
9+
public int Elves { get { return _elves; } }
10+
public int Dwarves { get { return _dwarves; } }
11+
public int Men { get { return _men; } }
12+
13+
public Distribution(int sauron, int elves, int dwarves, int men)
14+
{
15+
_sauron = sauron;
16+
_elves = elves;
17+
_dwarves = dwarves;
18+
_men = men;
19+
}
20+
}
21+
class Program
22+
{
23+
static void Main(string[] args)
24+
{
25+
Console.WriteLine("---DISTRIBUCIÓN DE ANILLOS---");
26+
Console.WriteLine("Ingresa un número entero de anillos a distribuir");
27+
int rings = 0;
28+
int.TryParse(Console.ReadLine(), out rings);
29+
if (rings == 0)
30+
{
31+
Console.WriteLine("El número de anillos no es válido...");
32+
return;
33+
}
34+
var distributions = DistributeRings(rings);
35+
if (distributions.Count() == 0)
36+
{
37+
Console.WriteLine("No se pudo distribuir los anillos correctamente...");
38+
return;
39+
}
40+
Console.WriteLine("---Posibles Distribuciones de los anillos---");
41+
foreach (var distribution in distributions)
42+
{
43+
Console.WriteLine($"Elfos: {distribution.Elves}, Enanos: {distribution.Dwarves} " +
44+
$"Hombres: {distribution.Men}, Sauron: {distribution.Sauron}");
45+
}
46+
Console.WriteLine("Distribucion media:");
47+
var mean = distributions[(int) distributions.Count() / 2];
48+
Console.WriteLine($"Elfos: {mean.Elves}, Enanos: {mean.Dwarves} " +
49+
$"Hombres: {mean.Men}, Sauron: {mean.Sauron}");
50+
}
51+
/*
52+
* 1. Los Elfos recibirán un número impar.
53+
* 2. Los Enanos un número primo.
54+
* 3. Los Hombres un número par.
55+
* 4. Sauron siempre uno.
56+
*/
57+
static List<Distribution> DistributeRings(int rings)
58+
{
59+
var distributions = new List<Distribution>();
60+
int sauron = 1;
61+
rings -= sauron;
62+
63+
for (int elves = 1; elves < rings; elves += 2)
64+
{
65+
for (int men = 2; men < rings; men += 2)
66+
{
67+
int dwarves = rings - elves - men;
68+
if (dwarves > 0 && IsPrime(dwarves))
69+
{
70+
var distribution = new Distribution(sauron, elves, dwarves, men);
71+
distributions.Add(distribution);
72+
}
73+
}
74+
}
75+
return distributions;
76+
}
77+
static bool IsPrime(int number)
78+
{
79+
if (number <= 1)
80+
return false;
81+
if (number == 2)
82+
return true;
83+
if (number % 2 == 0)
84+
return false;
85+
int limit = (int)Math.Sqrt(number);
86+
for (int i = 3; i < limit; i += 2)
87+
{
88+
if (number % i == 0)
89+
return false;
90+
91+
}
92+
return true;
93+
}
94+
95+
}

0 commit comments

Comments
 (0)