|
| 1 | +import java.util.*; |
| 2 | +import java.util.function.Consumer; |
| 3 | + |
| 4 | +/** |
| 5 | + * #35 REPARTIENDO LOS ANILLOS DE PODER |
| 6 | + * |
| 7 | + * @author martinbohorquez |
| 8 | + */ |
| 9 | +public class martinbohorquez { |
| 10 | + static List<Map<String, Race>> racesList = new LinkedList<>(); |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + Scanner sc = new Scanner(System.in); |
| 14 | + System.out.println("Introduce el número de anillos que deseas repartir:"); |
| 15 | + |
| 16 | + try { |
| 17 | + Integer totalRings = Integer.valueOf(sc.nextLine()); |
| 18 | + long duration1 = getDuration(martinbohorquez::distributeRings, totalRings); |
| 19 | + if (racesList.isEmpty()) System.out.println("No es posible distribuir los anillos de poder."); |
| 20 | + else { |
| 21 | + long duration2 = getDuration(martinbohorquez::displayDistributeRings, racesList); |
| 22 | + System.out.printf("La cantidad de posibles distribuciones es: %s%n", racesList.size()); |
| 23 | + long duration3 = getDuration(martinbohorquez::bestDistributeRings, racesList); |
| 24 | + double duration = (double) (duration1 + duration2 + duration3) / 1000; |
| 25 | + System.out.printf("Execution time in seconds: %d (ms) + %d (ms) + %d (ms) = %.2f seconds.%n", duration1, duration2, duration3, duration); |
| 26 | + } |
| 27 | + } catch (NumberFormatException e) { |
| 28 | + System.out.println("Debe ingresar un número entero de anillos!"); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private static <T> long getDuration(Consumer<T> method, T parameter) { |
| 33 | + long startTime = System.currentTimeMillis(); |
| 34 | + method.accept(parameter); // Run the passed method |
| 35 | + long endTime = System.currentTimeMillis(); |
| 36 | + return endTime - startTime; |
| 37 | + } |
| 38 | + |
| 39 | + private static Map<String, Race> getRaceMap() { |
| 40 | + Map<String, Race> races = new LinkedHashMap<>(); |
| 41 | + races.put("elves", new Race("Elfos")); |
| 42 | + races.put("dwarves", new Race("Enanos")); |
| 43 | + races.put("men", new Race("Hombres")); |
| 44 | + races.put("sauron", new Race("Sauron")); |
| 45 | + return races; |
| 46 | + } |
| 47 | + |
| 48 | + private static void distributeRings(Integer totalRings) { |
| 49 | + totalRings -= 1; |
| 50 | + for (int men = 2; men <= totalRings; men += 2) |
| 51 | + for (int elves = 1; elves <= (totalRings - men); elves += 2) { |
| 52 | + int dwarves = totalRings - men - elves; |
| 53 | + if (esPrimo(dwarves)) { |
| 54 | + Map<String, Race> raceMap = getRaceMap(); |
| 55 | + raceMap.get("elves").setRings(elves); |
| 56 | + raceMap.get("dwarves").setRings(dwarves); |
| 57 | + raceMap.get("men").setRings(men); |
| 58 | + raceMap.get("sauron").setRings(1); |
| 59 | + racesList.add(raceMap); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static void displayDistributeRings(List<Map<String, Race>> racesList) { |
| 65 | + racesList.stream() |
| 66 | + .map(Map::values) |
| 67 | + .forEach(System.out::println); |
| 68 | + } |
| 69 | + |
| 70 | + private static void bestDistributeRings(List<Map<String, Race>> racesList) { |
| 71 | + System.out.println("El conjunto de repartición mas equitativo es:"); |
| 72 | + racesList.stream().map(Map::values) |
| 73 | + .min(Comparator.comparingDouble(martinbohorquez::calcularDesviacionEstandar)) |
| 74 | + .ifPresent(System.out::println); |
| 75 | + } |
| 76 | + |
| 77 | + private static boolean esPrimo(Integer numero) { |
| 78 | + if (numero <= 1) return false; |
| 79 | + if (numero <= 3) return true; |
| 80 | + if (numero % 2 == 0) return false; |
| 81 | + for (int i = 3; i <= Math.sqrt(numero); i += 2) { |
| 82 | + if (numero % i == 0) return false; |
| 83 | + } |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + private static double calcularDesviacionEstandar(Collection<Race> races) { |
| 88 | + if (races.isEmpty()) return 0.0; |
| 89 | + |
| 90 | + double media = races.stream() |
| 91 | + .mapToInt(Race::getRings) |
| 92 | + .average() |
| 93 | + .orElse(0.0); |
| 94 | + |
| 95 | + double sumaCuadrados = races.stream() |
| 96 | + .mapToDouble(race -> Math.pow(race.getRings() - media, 2)) |
| 97 | + .sum(); |
| 98 | + |
| 99 | + return Math.sqrt(sumaCuadrados / races.size()); |
| 100 | + } |
| 101 | + |
| 102 | + private static class Race { |
| 103 | + private final String name; |
| 104 | + private Integer rings; |
| 105 | + |
| 106 | + public Race(String name) { |
| 107 | + this.name = name; |
| 108 | + } |
| 109 | + |
| 110 | + public Integer getRings() { |
| 111 | + return rings; |
| 112 | + } |
| 113 | + |
| 114 | + public void setRings(Integer rings) { |
| 115 | + this.rings = rings; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String toString() { |
| 120 | + return "'" + name + "': '" + rings + "'"; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
0 commit comments