试图解决这个难题,但没有找到正确的答案。这是生成的代码

时间:2016-05-07 12:36:44

标签: prolog clpfd

您的硬币值为5,10,20,50,100 其重量分别为2g,3g,10g,25g,50g。 你的钱包很弱,所以你不能超过391克的重量。 你可以在其里面放入3个具有相同价值的硬币。 你能说出钱包的最大价值是什么吗?

查询::: change([(Five,Ten,Twenty,Fifty,Hundred),W,S])

range(I,I,[I]).
range(I,K,[I|L]) :-             
    I < K,      
    I1 is I + 1,    
    range(I1,K,L).

coin(X,L) :-
    range(0,3,L1),
    member(X,L1).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

change([(Five,Ten,Twenty,Fifty,Hundred),W,S]) :- 
    coin(Five,L),
    coin(Ten,L),
    coin(Twenty,L),
    coin(Fifty,L),
    coin(Hundred,L),
    W1 = 50*Hundred + 25*Fifty + 10*Twenty +3*Ten+ 2*Five,
    S1 = 5*Five+ 10*Ten+ 20*Twenty + 50*Fifty + 100*Hundred,
    W1 < 391,
    W is W1,
    S is S1,
    maximum(S1).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
maximum(S1) :-
    S is S1,
    threshold(S),
    not( S1 < S).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

threshold(S1) :-
    M is S1,
    not( M < 451).

2 个答案:

答案 0 :(得分:3)

使用并运行以下查询:

:- use_module(library(clpfd)).

?- [Hundred,Fifty,Twenty,Ten,Five] ins 0..3,
   Weight #=< 391,
   Weight #=  50*Hundred + 25*Fifty + 10*Twenty +  3*Ten + 2*Five,
   Value  #= 100*Hundred + 50*Fifty + 20*Twenty + 10*Ten + 5*Five,
   labeling([max(Value)], [Hundred,Fifty,Twenty,Ten,Five]).
Hundred = 3, Fifty = 3, Twenty = 3, Ten = 3, Five = 3, Value = 555, Weight = 270 ;
Hundred = 3, Fifty = 3, Twenty = 3, Ten = 3, Five = 2, Value = 550, Weight = 268 ;
...

答案 1 :(得分:0)

'生成并测试'由库(aggregate)执行:

change(S) :-
    aggregate(max(V), P^W^(purse(P, W, V), W < 391), S).

purse(P, W, V) :-
    length(P, 5),
    maplist(between(0, 3), P),
    scalar(P, [2,3,10,25,50], W),
    scalar(P, [5,10,20,50,100], V).

scalar(Vs, Fs, S) :-
    foldl([V,F,V0,U]>>(U is V0+V*F), Vs, Fs, 0, S).

scalar / 3有点过度工程,如果你选择图书馆(scalar_product),可以考虑查看clpfd / 4