由于cnblogs的代码着色系统不支持erlang,所以就直接从博客上贴过来了,如果大家看的不习惯的话,就直接来我的博客上看吧
本文章为本人个人博客相应文章的镜像:
原文地址: http://www.greatony.com/index.php/2010/03/17/cashback-calculator-written-in-erlang/
今天在为公司买东西的时候,发现了qq的一个网站:QQ返利
后来又发现了新蛋网的qq返利的比率很有意思,最近又在学习erlang,所以就写个程序,看看对于这种算法类的程序,erlang写起来是否麻烦,一下是代码:
02 | - export ([solve/1, print_solution/1]). |
05 | get_cashback(N) when N =< 100 -> 0.76; |
06 | get_cashback(N) when N =< 300 -> 2.29; |
07 | get_cashback(N) when N =< 500 -> 5.35; |
08 | get_cashback(N) when N =< 800 -> 7.65; |
09 | get_cashback(N) when N =< 1000 -> 11.47; |
10 | get_cashback(N) when N =< 2000 -> 13.0; |
11 | get_cashback(N) when N =< 3000 -> 16.83; |
12 | get_cashback(N) when N =< 5000 -> 20.65; |
13 | get_cashback(N) when N =< 8000 -> 24.48; |
14 | get_cashback(N) when N =< 10000 -> 34.42; |
15 | get_cashback(_N) -> 42.07. |
18 | get_bill_cost({ Total , _}) when Total < 99 -> Total + 10.0 - get_cashback( Total ); |
19 | get_bill_cost({ Total , _}) -> Total - get_cashback( Total ). |
22 | get_solution_cost( Solution ) -> lists:foldl ( fun (X, Sum ) -> get_bill_cost(X) + Sum end , 0, Solution ). |
25 | reduce_comb( HeadList , [], Element ) -> [ HeadList ++ [[ Element ]]]; |
26 | reduce_comb( HeadList , [ Head | Rest ], Element ) -> [ HeadList ++ [[ Element | Head ]] ++ Rest | reduce_comb( HeadList ++ [ Head ], Rest , Element )]. |
29 | combination(_ Function , Default , []) -> Default ; |
30 | combination( Function , Default , [ Head ]) -> Function ([[ Head ]], Default ); |
31 | combination( Function , Default , [ Head | List ]) -> |
32 | combination( fun ( Combination , OldSolution ) -> |
33 | ReducedCombination = reduce_comb([], Combination , Head ), |
34 | lists:foldl ( Function , OldSolution , ReducedCombination ) |
38 | choose_solution( Solutions ) -> lists:foldl ( |
39 | fun ( NewSolution , ExistSolution ) -> |
43 | no_solution -> NewSolution ; |
44 | { OldCost , _} when OldCost > Cost -> { Cost , Solution }; |
47 | no_solution -> ExistSolution |
49 | end , no_solution, Solutions ). |
52 | solve([]) -> {ok, 0, []}; |
53 | solve([ Price ]) -> {ok, get_bill_cost({ Price , [ Price ]}), [[ Price ]]}; |
54 | solve( PriceList ) -> combination( |
55 | fun ( Combination , Origional ) -> |
56 | Solution = lists:map ( fun ( Bill ) -> { lists:sum ( Bill ), Bill } end , Combination ), |
57 | choose_solution([{get_solution_cost( Solution ), Solution } , Origional ]) |
58 | end , no_solution, PriceList ). |
61 | print_bill({ Total , Items }) -> |
62 | io:fwrite ( " Bill Total: ~p, Cashback: ~p, Real: ~p~n" , [ Total , get_cashback( Total ), get_bill_cost({ Total , Items })]), |
63 | lists:foreach ( fun ( Item ) -> io:fwrite ( " ~p~n" , [ Item ]) end , Items ), |
67 | print_solution({ Cost , Solution }) -> |
68 | io:fwrite ( "Solution Total: ~p, Bill Count: ~p~n" , [ Cost , length( Solution )]), |
69 | lists:foreach ( fun ( Bill ) -> print_bill( Bill ) end , Solution ), |
用法:
1 | bne:print_solution ( bne:solve ([64, 44, 100, 10, 29])). |
Erlang确实是个很优秀的语言,特别是解决这种问题的时候。
我几乎就只是在描述问题,而不是在解决问题,虽然这还需要我花一点时间去适应
在下一篇文章里,我会着重讲排列组合的实现方式