由于cnblogs的代码着色系统不支持erlang,所以就直接从博客上贴过来了,如果大家看的不习惯的话,就直接来我的博客上看吧

本文章为本人个人博客相应文章的镜像:

原文地址: http://www.greatony.com/index.php/2010/03/17/cashback-calculator-written-in-erlang/ 

 

今天在为公司买东西的时候,发现了qq的一个网站:QQ返利

后来又发现了新蛋网的qq返利的比率很有意思,最近又在学习erlang,所以就写个程序,看看对于这种算法类的程序,erlang写起来是否麻烦,一下是代码:

01-module(bne).
02-export([solve/1, print_solution/1]).
03 
04% Calculate the cashback of a bill
05get_cashback(N) when N =< 100 -> 0.76;
06get_cashback(N) when N =< 300 -> 2.29;
07get_cashback(N) when N =< 500 -> 5.35;
08get_cashback(N) when N =< 800 -> 7.65;
09get_cashback(N) when N =< 1000 -> 11.47;
10get_cashback(N) when N =< 2000 -> 13.0;
11get_cashback(N) when N =< 3000 -> 16.83;
12get_cashback(N) when N =< 5000 -> 20.65;
13get_cashback(N) when N =< 8000 -> 24.48;
14get_cashback(N) when N =< 10000 -> 34.42;
15get_cashback(_N) -> 42.07.
16 
17% Calculate the real cost of a bill
18get_bill_cost({Total, _}) when Total < 99 -> Total + 10.0 - get_cashback(Total);
19get_bill_cost({Total, _}) -> Total - get_cashback(Total).
20 
21% Calculate the total cost of a solution
22get_solution_cost(Solution) -> lists:foldl(fun(X, Sum) -> get_bill_cost(X) + Sumend, 0, Solution).
23 
24% make combinations with a series of combination and a new element
25reduce_comb(HeadList, [], Element) -> [HeadList ++ [[Element]]];
26reduce_comb(HeadList, [Head Rest], Element) -> [HeadList ++ [[Element Head]] ++Rest | reduce_comb(HeadList ++ [Head], RestElement)].
27 
28% call Function on each combination of the 3rd argument, and the Default, the return value will overwrite the Default value
29combination(_FunctionDefault, []) -> Default;
30combination(FunctionDefault, [Head]) -> Function([[Head]], Default);
31combination(FunctionDefault, [Head List]) ->
32    combination(fun(CombinationOldSolution) ->
33        ReducedCombination = reduce_comb([], CombinationHead),
34        lists:foldl(FunctionOldSolutionReducedCombination)
35    endDefaultList).
36 
37% choose a solution from a set of solution
38choose_solution(Solutions) -> lists:foldl(
39    fun(NewSolutionExistSolution) ->
40        case NewSolution of
41            {CostSolution} ->
42                case ExistSolution of
43                    no_solution -> NewSolution;
44                    {OldCost, _} when OldCost Cost -> {CostSolution};
45                    _ -> ExistSolution
46                end;
47            no_solution -> ExistSolution
48        end
49    end, no_solution, Solutions).
50 
51% solve problem
52solve([]) -> {ok, 0, []};
53solve([Price]) -> {ok, get_bill_cost({Price, [Price]}), [[Price]]};
54solve(PriceList) -> combination(
55    fun(CombinationOrigional) ->
56        Solution lists:map(fun(Bill) -> {lists:sum(Bill), Billend,Combination),
57        choose_solution([{get_solution_cost(Solution), Solution} , Origional])
58    end, no_solution, PriceList).
59 
60% print a bill
61print_bill({TotalItems}) ->
62    io:fwrite("    Bill Total: ~p, Cashback: ~p, Real: ~p~n", [Total, get_cashback(Total), get_bill_cost({TotalItems})]),
63    lists:foreach(fun(Item) -> io:fwrite("        ~p~n", [Item]) endItems),
64    io:fwrite("~n").
65 
66% print the solution
67print_solution({CostSolution}) ->
68    io:fwrite("Solution Total: ~p, Bill Count: ~p~n", [Cost, length(Solution)]),
69    lists:foreach(fun(Bill) -> print_bill(BillendSolution),
70    io:fwrite("~n").

用法:

1bne:print_solution(bne:solve([64, 44, 100, 10, 29])).

Erlang确实是个很优秀的语言,特别是解决这种问题的时候。

我几乎就只是在描述问题,而不是在解决问题,虽然这还需要我花一点时间去适应

在下一篇文章里,我会着重讲排列组合的实现方式

 

posted on 2010-03-17 01:03  TonyHuang  阅读(841)  评论(0编辑  收藏  举报