1

Every letter below in the puzzle uniquely represent one of the 10 digits in 0, 1, …, 9. No two letters represent the same digit. For each word below in the puzzle, the first letter is not 0.

Ex: SHINE - THAN == KNIT

All I've got for the code is this...

:- lib(ic).

exampleOne(List) :-
    List = [S, H, I, N, E, T, A, K],
    List :: 0..9,
    diff_list(List),
    (10000*S - 1000*H - 100*I - 10*N - E)
    - (1000*T - 100*H - 10*A - N)
    $= (1000*K - 100*N - 10*I - T),
    S $\= 0, T $\= 0, K $\= 0,
    shallow_backtrack(List).

shallow_backtrack(List) :-
    ( foreach(Var, List) do once(indomain(Var)) ).

diff_list(List) :-
    ( fromto(List, [X|Tail], Tail, []) do
          ( fromto(Y, Tail, param(X) do
              X $\= Y
          )
    ).

compareLists(List) :-
    length(List, N),
    ( foreach(Input1, List), count(I, 1, N), param(N, List)
        do
             ( foreach(Input2, List), count(J, 1, N), param(List, Input1, I, N)
                  do
                      ( ( I $\= J, Input1 $\= Input2 )
                            -> true;
                            fail
                      )
             )
     ).

I'm kinda stuck on this part. So when I ran the code without the compareLists(List) function, the result gives me "No". So when I added the compareLists(List) function, the answer still gives me "No". I was wondering if "No" is the right answer or did I write something wrong in my code? Any help is appreciated. Thanks!

Thanks!

4

1 回答 1

0

模型中几乎所有的缺点(除了一个)都必须是优点。即使“SHINE - THAN”中有减号,SHINE 仍然是 (10000*S + 1000*H + 100*I + 10*N + E) - 这只是简单的数学运算。

也无需重新发明标准谓词alldifferentdiff_list在您的代码中命名)和labelingshallow_backtrack在您的代码中命名)。不仅你的程序有很多不需要的代码,而且标准谓词在许多情况下会更有效,也更灵活。

这是使用标准 ic 库谓词的完整更正程序:

:- lib(ic).

exampleOne(List) :-
    List = [S, H, I, N, E, T, A, K],
    List :: 0..9,
    alldifferent(List),
    (10000*S + 1000*H + 100*I + 10*N + E)
    - (1000*T + 100*H + 10*A + N)
    $= (1000*K + 100*N + 10*I + T),
    S $\= 0, T $\= 0, K $\= 0,
    labeling(List).
于 2014-05-18T06:14:28.080 回答