Advertisement
JosepRivaille

LI - Coins

Jun 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.73 KB | None | 0 0
  1. :- use_module(library(clpfd)).
  2.  
  3. ejemplo(0,   26, [1,2,5,10]).  % Solution: [1,0,1,2]
  4. ejemplo(1,  361, [1,2,5,13,17,35,157]).
  5.  
  6. calculateAmmount([], [], 0, 0).
  7. calculateAmmount([C1|C], [V1|V], M, S) :-
  8.     calculateAmmount(C, V, M1, S1),
  9.     M #= M1 + C1*V1,
  10.     S #= S1 + V1.
  11.  
  12. doExample(E) :-
  13.     ejemplo(E, Amount, Coins),
  14.     nl, write('Paying amount '), write(Amount), write(' using the minimal number of coins of values '), write(Coins), nl,
  15.     length(Coins, N),
  16.     length(Vars, N), % get list of N prolog vars    
  17.     Vars ins 0..Amount,
  18.     calculateAmmount(Coins, Vars, Money, Size),
  19.     Money #= Amount,
  20.     labeling([min(Size)], Vars),
  21.     write(Vars), nl.
  22.  
  23. main:-
  24.     doExample(0),
  25.     doExample(1),
  26.     halt.
  27.  
  28. % JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement