背包问题的haskell解法
2011-04-24 08:28
288 查看
-- file: knapsack1.hs
maxVal [] [] _ = 0
maxVal (w:ws) (v:vs) aW =
if w > aW
then
without_it
else
max without_it (with_it+v)
where
without_it = maxVal ws vs aW
with_it = maxVal ws vs (aW-w)
weights = [1, 5, 3, 4]
vals = [15, 10, 9, 5]
evalue = maxVal weights vals 8
-- file: knapsack2.hs
maxVal [] [] _ = (0, 0)
maxVal (w:ws) (v:vs) aW =
if w > aW
then
(without_it, nc1+1)
else
(max without_it (with_it+v), nc1+nc2+1)
where
(without_it, nc1) = maxVal ws vs aW
(with_it, nc2) = maxVal ws vs (aW-w)
weights = [1, 5, 3, 4]
vals = [15, 10, 9, 5]
evalue = maxVal weights vals 8
-- file: knapsack3.hs
maxVal [] [] _ = (0, 0, [])
maxVal (w:ws) (v:vs) aW =
if w > aW then
(without_it, nc1+1, 0:xs1)
else if without_it > with_it then
(without_it, nc1+nc2+1, 0:xs1)
else
(with_it, nc1+nc2+1, 1:xs2)
where
(without_it, nc1, xs1) = maxVal ws vs aW
(with_it', nc2, xs2) = maxVal ws vs (aW-w)
with_it = with_it' + v
weights = [1, 5, 3, 4]
vals = [15, 10, 9, 5]
evalue = maxVal weights vals 8
maxVal [] [] _ = 0
maxVal (w:ws) (v:vs) aW =
if w > aW
then
without_it
else
max without_it (with_it+v)
where
without_it = maxVal ws vs aW
with_it = maxVal ws vs (aW-w)
weights = [1, 5, 3, 4]
vals = [15, 10, 9, 5]
evalue = maxVal weights vals 8
-- file: knapsack2.hs
maxVal [] [] _ = (0, 0)
maxVal (w:ws) (v:vs) aW =
if w > aW
then
(without_it, nc1+1)
else
(max without_it (with_it+v), nc1+nc2+1)
where
(without_it, nc1) = maxVal ws vs aW
(with_it, nc2) = maxVal ws vs (aW-w)
weights = [1, 5, 3, 4]
vals = [15, 10, 9, 5]
evalue = maxVal weights vals 8
-- file: knapsack3.hs
maxVal [] [] _ = (0, 0, [])
maxVal (w:ws) (v:vs) aW =
if w > aW then
(without_it, nc1+1, 0:xs1)
else if without_it > with_it then
(without_it, nc1+nc2+1, 0:xs1)
else
(with_it, nc1+nc2+1, 1:xs2)
where
(without_it, nc1, xs1) = maxVal ws vs aW
(with_it', nc2, xs2) = maxVal ws vs (aW-w)
with_it = with_it' + v
weights = [1, 5, 3, 4]
vals = [15, 10, 9, 5]
evalue = maxVal weights vals 8
相关文章推荐
- 0/1背包问题解法
- 01背包问题(当有的背包重量是非整数时)的递归(优化成动态规划+再用滚动数组优化)解法+一些动态规划(递归,搜索)的高级技巧
- 分组背包问题解法
- 0-1背包问题及其动态规划求解之二——王晓东的书本解法
- 背包问题的非递归解法
- 0-1背包问题的动态规划解法
- 背包问题的c++解法
- 完全背包问题(二进制解法)
- 0-1背包问题的两种解法(回溯法和动态规划)
- hdu 1226(背包问题解法)
- 通俗理解0-1背包问题解法
- 为什么0-1背包问题是NP完全问题,但该问题还有动态规划的解法?
- 背包问题的递归和非递归的解法
- 0/1背包问题,java解法
- 0-1背包问题的动态规划解法
- [算法]简单的背包问题递归解法,C语言实现
- 背包分组问题的解法
- 0/1背包问题,java解法
- [算法]数据结构算法背包问题解法之递归解法,C语言实现
- 01背包问题的一维数组解法