Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package es.pruebasjava;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- @SuppressWarnings("rawtypes")
- public class PruebasJava {
- /*
- * ¿Cómo optimizarías tu premio para llevarte la
- * mayor cantidad posible de tus artículos más deseados? Hay que tener en cuenta que cada artículo
- * es
- * indivisible, tiene un valor y ocupa un espacio. Además, no se permite rebasar el volumen de cada
- * uno de
- * los carritos. Buscamos una función que resuelva el problema. Los tamaños de los contenedores
- * (carritos)
- * y los objetos, así como los valores de éstos últimos, serían inputs de dicha función.
- */
- public static void main(String[] args) {
- ArrayList<Article> arrayList = new ArrayList<>();
- arrayList.add(new Article("Placa Base", 4, 6));
- arrayList.add(new Article("Fuente Alimentacion", 6, 5));
- arrayList.add(new Article("Ram", 2, 7));
- arrayList.add(new Article("CPU", 1, 8));
- arrayList.add(new Article("GPU", 5, 12));
- int pesoMaxCarrito = 10;
- implementacion2(arrayList, pesoMaxCarrito);
- }
- @SuppressWarnings("unchecked")
- private static void implementacion1(ArrayList<Article> arrayList, int pesoMaxCarrito) {
- Comparator cmp = new Comparator<Article>() {
- @Override
- public int compare(Article x, Article y) {
- return x.getValue() / x.getWeight() - y.getValue() / y.getWeight();
- }
- };
- ArrayList<Article> mochila = new ArrayList<Article>();
- Collections.sort(arrayList, cmp); // ordena usando el comparador anterior
- Collections.reverse(arrayList); // reversa el orden de los elementos
- double pesoMochila = 0;
- int posicion = 0;
- while (pesoMochila < pesoMaxCarrito && posicion < arrayList.size()) {
- Article tmp = arrayList.get(posicion);
- if (pesoMochila + tmp.getWeight() <= pesoMaxCarrito) {
- mochila.add(tmp);
- pesoMochila += tmp.getWeight();
- }
- posicion++;
- }
- mochila.forEach(x -> System.out.println(x.toString()));
- }
- private static void implementacion2(ArrayList<Article> arrayList, double total) {
- ArrayList<Article> mochila = new ArrayList<Article>();
- double total1 = total;
- while (total1 >= 0.00) {
- int posicionMoneda = getBestValue(total1, arrayList); // Buscamos la mejor moneda asociada al total
- if (!existsOnMochila(arrayList, mochila)) {
- total1 = total1 - arrayList.get(posicionMoneda)
- .getWeight(); // Restamos la moneda usada al total
- mochila.add(arrayList.get(posicionMoneda));
- }
- }
- // return contador;
- mochila.forEach(x -> System.out.println(x.toString()));
- System.out.println("Terminado");
- }
- private static boolean existsOnMochila(ArrayList<Article> arrayList, ArrayList<Article> mochila) {
- for (Article articleMochila : mochila) {
- for (Article articleArrayList : arrayList) {
- if (articleMochila.getName()
- .equals(articleArrayList.getName())) {
- return true;
- }
- }
- }
- return false;
- }
- private static int getBestValue(double total, ArrayList<Article> monedas) {
- double resultado = total;
- int contador = 0;
- for (int i = 0; i < monedas.size(); i++)
- if ((resultado > (total - monedas.get(i)
- .getValue()))
- && (total - monedas.get(i)
- .getValue() >= 0)) {
- resultado = (total - monedas.get(i)
- .getValue());
- contador = i;
- }
- return contador;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement