Advertisement
Guest User

Untitled

a guest
May 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. package es.pruebasjava;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6.  
  7. @SuppressWarnings("rawtypes")
  8. public class PruebasJava {
  9.   /*
  10.    * ¿Cómo optimizarías tu premio para llevarte la
  11.    * mayor cantidad posible de tus artículos más deseados? Hay que tener en cuenta que cada artículo
  12.    * es
  13.    * indivisible, tiene un valor y ocupa un espacio. Además, no se permite rebasar el volumen de cada
  14.    * uno de
  15.    * los carritos. Buscamos una función que resuelva el problema. Los tamaños de los contenedores
  16.    * (carritos)
  17.    * y los objetos, así como los valores de éstos últimos, serían inputs de dicha función.
  18.    */
  19.   public static void main(String[] args) {
  20.     ArrayList<Article> arrayList = new ArrayList<>();
  21.     arrayList.add(new Article("Placa Base", 4, 6));
  22.     arrayList.add(new Article("Fuente Alimentacion", 6, 5));
  23.     arrayList.add(new Article("Ram", 2, 7));
  24.     arrayList.add(new Article("CPU", 1, 8));
  25.     arrayList.add(new Article("GPU", 5, 12));
  26.     int pesoMaxCarrito = 10;
  27.     implementacion2(arrayList, pesoMaxCarrito);
  28.   }
  29.  
  30.   @SuppressWarnings("unchecked")
  31.   private static void implementacion1(ArrayList<Article> arrayList, int pesoMaxCarrito) {
  32.     Comparator cmp = new Comparator<Article>() {
  33.       @Override
  34.       public int compare(Article x, Article y) {
  35.         return x.getValue() / x.getWeight() - y.getValue() / y.getWeight();
  36.       }
  37.     };
  38.     ArrayList<Article> mochila = new ArrayList<Article>();
  39.     Collections.sort(arrayList, cmp);  // ordena usando el comparador anterior
  40.     Collections.reverse(arrayList);   // reversa el orden de los elementos
  41.     double pesoMochila = 0;
  42.     int posicion = 0;
  43.     while (pesoMochila < pesoMaxCarrito && posicion < arrayList.size()) {
  44.       Article tmp = arrayList.get(posicion);
  45.       if (pesoMochila + tmp.getWeight() <= pesoMaxCarrito) {
  46.         mochila.add(tmp);
  47.         pesoMochila += tmp.getWeight();
  48.       }
  49.       posicion++;
  50.     }
  51.     mochila.forEach(x -> System.out.println(x.toString()));
  52.   }
  53.  
  54.   private static void implementacion2(ArrayList<Article> arrayList, double total) {
  55.     ArrayList<Article> mochila = new ArrayList<Article>();
  56.     double total1 = total;
  57.     while (total1 >= 0.00) {
  58.       int posicionMoneda = getBestValue(total1, arrayList); // Buscamos la mejor moneda asociada al total
  59.       if (!existsOnMochila(arrayList, mochila)) {
  60.         total1 = total1 - arrayList.get(posicionMoneda)
  61.                                    .getWeight(); // Restamos la moneda usada al total
  62.         mochila.add(arrayList.get(posicionMoneda));
  63.       }
  64.     }
  65.     // return contador;
  66.     mochila.forEach(x -> System.out.println(x.toString()));
  67.     System.out.println("Terminado");
  68.   }
  69.  
  70.   private static boolean existsOnMochila(ArrayList<Article> arrayList, ArrayList<Article> mochila) {
  71.     for (Article articleMochila : mochila) {
  72.       for (Article articleArrayList : arrayList) {
  73.         if (articleMochila.getName()
  74.                           .equals(articleArrayList.getName())) {
  75.           return true;
  76.         }
  77.       }
  78.     }
  79.     return false;
  80.   }
  81.  
  82.   private static int getBestValue(double total, ArrayList<Article> monedas) {
  83.     double resultado = total;
  84.     int contador = 0;
  85.     for (int i = 0; i < monedas.size(); i++)
  86.       if ((resultado > (total - monedas.get(i)
  87.                                        .getValue()))
  88.           && (total - monedas.get(i)
  89.                              .getValue() >= 0)) {
  90.         resultado = (total - monedas.get(i)
  91.                                     .getValue());
  92.         contador = i;
  93.       }
  94.     return contador;
  95.   }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement