![]() ![]() ![]() To_Unbounded_String ( "trousers" ), 48, 10, False ), ( US. To_Unbounded_String ( "t-shirt" ), 24, 15, False ), ( US. To_Unbounded_String ( "camera" ), 32, 30, False ), ( US. To_Unbounded_String ( "suntan cream" ), 11, 70, False ), ( US. To_Unbounded_String ( "beer" ), 52, 10, False ), ( US. To_Unbounded_String ( "cheese" ), 23, 30, False ), ( US. To_Unbounded_String ( "apple" ), 39, 40, False ), ( US. To_Unbounded_String ( "banana" ), 27, 60, False ), ( US. To_Unbounded_String ( "glucose" ), 15, 60, False ), ( US. To_Unbounded_String ( "sandwich" ), 50, 160, False ), ( US. To_Unbounded_String ( "compass" ), 13, 35, False ), ( US. Weight end if end loop end end Solve_Knapsack_01 All_Items : Item_Array := ( ( US. Taken := True Rest := Rest - Items ( I ). Value ) end if end loop end loop declare Rest : Natural := Weight_Limit begin for I in reverse Items ' Range loop if W ( I, Rest ) /= W ( I - 1, Rest ) then Items ( I ). Weight > J then W ( I, J ) := W ( I - 1, J ) else W ( I, J ) := Max ( W ( I - 1, J ), W ( I - 1, J - Items ( I ). Weight_Limit ) of Natural W : W_Array := ( others => ( others => 0 )) begin - fill W for I in Items ' Range loop for J in 1. Value end if end loop return Sum end Total_Value function Max ( Left, Right : Natural ) return Natural is begin if Right > Left then return Right else return Left end if end Max procedure Solve_Knapsack_01 ( Items : in out Item_Array Weight_Limit : Positive := 400 ) is type W_Array is array ( 0. Weight end if end loop return Sum end Total_Weight function Total_Value ( Items : Item_Array Untaken : Boolean := False ) return Natural is Sum : Natural := 0 begin for I in Items ' Range loop if Untaken or else Items ( I ). ![]() Unbounded_String Weight : Positive Value : Positive Taken : Boolean end record type Item_Array is array ( Positive range ) of Item function Total_Weight ( Items : Item_Array Untaken : Boolean := False ) return Natural is Sum : Natural := 0 begin for I in Items ' Range loop if Untaken or else Items ( I ). With Ada.Text_IO with procedure Knapsack_01 is package US renames type Item is record Name : US. IF C,R0,EQ,=F'1' THEN if tstbit(xb,i)=1 then IF C,R8,LE,MAXW,AND,C,R9,GT,XV THEN if wxv then IF C,R0,EQ,=F'1' THEN if tstbit(i,j)=1 then and a total weight of #.’.format(val, -wt)) Show which items the tourist can carry in his knapsack so that their total weight does notĮxceed 400 dag, and their total value is maximized. He may not cut or diminish the items, so he can only take whole units of any item. The tourist can choose to take any combination of items from the list, He then decides to add columns to his initial list detailing their weights and a numerical value representing how important the item is for the trip. He creates a list of what he wants to bring for the trip but the total weight of all items is too much. He has a good knapsack for carrying things, but knows that he can carry a maximum of only 4kg in it, and it will have to last the whole day. They will go to the mountains to see the wonders of nature, so he needs to pack well for the trip. You are encouraged to solve this task according to the task description, using any language you may know.Ī tourist wants to make a good trip at the weekend with his friends.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |