由于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
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.
16  
17 % Calculate the real cost of a bill
18 get_bill_cost({Total, _}) when Total < 99 -> Total + 10.0 - get_cashback(Total);
19 get_bill_cost({Total, _}) -> Total - get_cashback(Total).
20  
21 % Calculate the total cost of a solution
22 get_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
25 reduce_comb(HeadList, [], Element) -> [HeadList ++ [[Element]]];
26 reduce_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
29 combination(_FunctionDefault, []) -> Default;
30 combination(FunctionDefault, [Head]) -> Function([[Head]], Default);
31 combination(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
38 choose_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
52 solve([]) -> {ok, 0, []};
53 solve([Price]) -> {ok, get_bill_cost({Price, [Price]}), [[Price]]};
54 solve(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
61 print_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
67 print_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").

相关文章:

  • 2021-12-02
  • 2021-08-29
  • 2021-04-02
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-07-19
  • 2021-07-29
  • 2021-05-19
  • 2021-04-04
相关资源
相似解决方案