Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.69 KB | None | 0 0
  1. % The Postage Stamp Problem
  2. % Running GNU Prolog 1.3.1 on Windows 7
  3. % Given the maximum number of stamps and a set of stamp denominations
  4. % This program should print to screen the maximum amount of coverage
  5. % that the denominations can fill on an envelope
  6. % so if you have test(1, 5, [1, 4, 12, 21]).
  7. % 1 is the test case; 5 is the max stamps on the envelope
  8. % 1, 4, 12, 21 are the stamp denominations
  9. % That would return 71, as 72 can not be created with a max size of 5
  10. % allowed stamps on the envelope.
  11.  
  12. test(1, 5, [1, 4, 12, 21]).      % from in01.txt
  13. test(2, 10, [1, 7, 16, 31, 88]). % from in01.txt
  14. test(3, 6, [1, 5, 7, 8]).        % from in01.txt
  15. test(4, 5, [1, 4]).              % from in02.txt
  16. test(5, 6, [1, 2, 3, 7, 11]).    % from in03.txt
  17. test(6, 7, [1, 5, 9]).           % from in03.txt
  18. test(7, 6, [2, 3, 7, 11]).       % from in05.txt
  19. test(8, 7, [5, 9]).              % from in05.txt
  20. test(9, 10, [1]).                % from in06.txt
  21. test(10, 10, [1, 3]).            % from in06.txt
  22.  
  23. runtest(N, C) :- test(N, Width, Stamps), maxcoverage(C, Width, Stamps).
  24.  
  25. maxcoverage(Max, StampsCount, Stamps):-
  26.     maxcoverage(0, StampsCount, Stamps, Max),
  27.     Max > 0.
  28.  
  29. maxcoverage(Previous, StampsCount, Stamps, Max):-
  30.     Current is Previous + 1,
  31.     (realize(StampsCount, Stamps, Current) ->       % Tries current value.
  32.         maxcoverage(Previous, StampsCount, Stamps, Max) % When succeeds, tries more.
  33.     ;
  34.         Max = Previous                              % Else returns prvious value as max.
  35.     ).
  36. realize(StampsCount, Stamps, Value):-
  37.     (Value = 0 ->
  38.         true
  39.     ;
  40.         StampsCount > 0,
  41.         Value > 0,
  42.         member(Stamp, Stamps),
  43.         ValCount is Value - Stamp,
  44.         VValue is Value - 1,
  45.         realize(VValue, Stamps, ValCount)
  46.     ).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement