Skip to content
Snippets Groups Projects
Commit 5bac2569 authored by OUATTARA Sie's avatar OUATTARA Sie
Browse files

Merge branch 'dev1' into 'main'

Dev1

See merge request !3
parents 179253d6 89fb0e0a
No related branches found
No related tags found
1 merge request!3Dev1
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="18" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -2,7 +2,9 @@ import java.util.List;
public class App {
public static void main(String[] args) throws Exception {
Backpack backpack= new InstanceReader().read("src/sac0");
System.out.println(backpack);
Backpack backpack= new InstanceReader().read("src/sacTest");
backpack.sortByRatio();
backpack.solutionFractionnelle();
System.out.println("solution fractionnelle :" + backpack.getSfValuee());
}
}
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;
......@@ -12,4 +15,34 @@ public class Backpack {
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;
}
}
......@@ -23,5 +23,4 @@ public class InstanceReader {
return new Backpack(capacity,loots);
}
}
public class Loot {
int weight, value;
float ratio;
public Loot(int weight, int value) {
this.weight = weight;
this.value = value;
this.ratio = (float) value/weight;
}
public String toString(){
return weight+" "+value;
return weight+" "+value+" "+ratio;
}
public float getRatio(){
return ratio;
}
public int getWeight(){
return weight;
}
public int getValue(){
return value;
}
}
15
5 40
8 48
4 36
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment