September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,4 +1,5 @@
with Ada.Text_IO;
with Ada.Float_Text_IO;
with Ada.Strings.Unbounded;
procedure Knapsack_Continuous is
@ -23,7 +24,7 @@ procedure Knapsack_Continuous is
Sum : Float := 0.0;
begin
for I in Items'Range loop
Sum := Sum + Items (I).Weight * Items (I).Taken;
Sum := Sum + Items (I).Taken;
end loop;
return Sum;
end Total_Weight;
@ -32,7 +33,7 @@ procedure Knapsack_Continuous is
Sum : Float := 0.0;
begin
for I in Items'Range loop
Sum := Sum + Float (Items (I).Value) * Items (I).Taken;
Sum := Sum + Float (Items (I).Value) / Items(I).Weight * Items (I).Taken;
end loop;
return Sum;
end Total_Value;
@ -71,8 +72,7 @@ procedure Knapsack_Continuous is
end loop;
end;
end Solve_Knapsack_Continuous;
All_Items : Item_Array :=
All_Items : Item_Array :=
((US.To_Unbounded_String ("beef"), 3.8, 36, 0.0),
(US.To_Unbounded_String ("pork"), 5.4, 43, 0.0),
(US.To_Unbounded_String ("ham"), 3.6, 90, 0.0),
@ -85,18 +85,18 @@ procedure Knapsack_Continuous is
begin
Solve_Knapsack_Continuous (All_Items, 15.0);
Ada.Text_IO.Put_Line
("Total Weight: " & Float'Image (Total_Weight (All_Items)));
Ada.Text_IO.Put_Line
("Total Value: " & Float'Image (Total_Value (All_Items)));
Ada.Text_IO.Put ("Total Weight: ");
Ada.Float_Text_IO.Put (Total_Weight (All_Items), 0, 2, 0);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put ("Total Value: ");
Ada.Float_Text_IO.Put (Total_Value (All_Items), 0, 2, 0);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line ("Items:");
for I in All_Items'Range loop
if All_Items (I).Taken > 0.0 then
Ada.Text_IO.Put_Line
(" " &
Float'Image (All_Items (I).Taken) &
" of " &
US.To_String (All_Items (I).Name));
Ada.Text_IO.Put (" ");
Ada.Float_Text_IO.Put (All_Items (I).Taken, 0, 2, 0);
Ada.Text_IO.Put_Line (" of " & US.To_String (All_Items (I).Name));
end if;
end loop;
end Knapsack_Continuous;

View file

@ -0,0 +1,34 @@
fn main() {
let items: [(&str, f32, u8); 9] = [
("beef", 3.8, 36),
("pork", 5.4, 43),
("ham", 3.6, 90),
("greaves", 2.4, 45),
("flitch", 4.0, 30),
("brawn", 2.5, 56),
("welt", 3.7, 67),
("salami", 3.0, 95),
("sausage", 5.9, 98),
];
let mut weight: f32 = 15.0;
let mut values: Vec<(&str, f32, f32)> = Vec::new();
for item in &items {
values.push((item.0, f32::from(item.2) / item.1, item.1));
}
values.sort_by(|a, b| (a.1).partial_cmp(&b.1).unwrap());
values.reverse();
for choice in values {
if choice.2 <= weight {
println!("Grab {:.1} kgs of {}", choice.2, choice.0);
weight -= choice.2;
if (choice.2 - weight).abs() < std::f32::EPSILON {
return;
}
} else {
println!("Grab {:.1} kgs of {}", weight, choice.0);
return;
}
}
}