Select Git revision
DrawerState.java
Forked from
COUETOUX Basile / graphic-2020
Source project has a limited visibility.
-
MANSOUR Chadi authoredMANSOUR Chadi authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Backpack.java 1.20 KiB
import java.util.Comparator;
import java.util.List;
public class Backpack {
int capacity;
List<Loot> loots;
int sfValuee = 0;
int sfWeight = 0;
public Backpack(int capacity, List<Loot> loots) {
this.capacity = capacity;
this.loots = loots;
}
public String toString(){
return capacity +"\n"+loots;
}
/**
Méthode pour trier les objets par ordre décroissant selon leur ratio
*/
public void sortByRatio(){
loots.sort(Comparator.comparing(Loot::getRatio).reversed());
}
public void solutionFractionnelle(){
this.sortByRatio();
for(Loot loot : loots){
if(sfWeight + loot.getWeight() <= this.capacity){
// Si l'objet entier peut entrer dans le sac
sfValuee += loot.getValue();
sfWeight += loot.getWeight();
}
else {
// Si l'objet ne peut pas entrer entièrement dans le sac
int remainingWeight = this.capacity - sfWeight;
sfValuee += (int) (loot.getRatio() * remainingWeight);
break;
}
}
}
public int getSfValuee() {
return sfValuee;
}
}