Knapsack problem/Continuous: Difference between revisions

→‎{{header|Haskell}}: Then again, if we're going to use exact arithmetic, we might as well give exact answers.
(Added Haskell.)
(→‎{{header|Haskell}}: Then again, if we're going to use exact arithmetic, we might as well give exact answers.)
Line 42:
import Data.List (sortBy)
import Data.Ord (comparing)
import Data.Ratio (numerator, denominator)
import Text.Printf
 
Line 70 ⟶ 71:
main = do
forM_ solution $ \(w, b) ->
putStrLn $ showRat w ++printf "%s kg of %s\n" ++(mixedNum w) (itemName b)
putStrLn $printf "Total value: %s\n" ++$ showRatmixedNum $ (sum $ map f solution)
where f (w, Bounty _ v wtot) = v * (w / wtot)
showRatmixedNum q = printfif "%.2f"b x== 0
then show a
where x = fromInteger (round $ q * 100) / 100 :: Double</lang>
else printf "%d %d/%d" a (numerator b) (denominator b)
where a = floor q
b = q - toEnum a</lang>
 
=={{header|Java}}==
845

edits