gpt4 book ai didi

prolog - 列表总和必须为 16

转载 作者:行者123 更新时间:2023-12-02 09:18:14 24 4
gpt4 key购买 nike

我现在被这个问题困了两个小时了!我在 prolog 中的问题是我在以声明的方式思考!
所以我的问题是让 prolog 生成一个等于 N 的列表!列表的大小为 5。

somme([],0) :- !.
somme([X|L],S) :- somme(L,S1), S is S1+X.


generateList(L,RES) :- C1 is random(4)+1,
C2 is random(5)+1,
C3 is random(6)+1,
append(L,[C1,C2,C3],RES).


go(L) :- generateList([2,3],L), somme(L,S), S \==16, go(L).
go(L) :- generateList([2,3],L), somme(L,S), S == 16,write(L).

我们假设第 2 个元素是 2 和 3。

最佳答案

您还可以使用 CLP(FD) 来描述此类列表:

:- use_module(library(clpfd)).

numbers(L) :-
L=[2,3,_A,_B,_C], % list has 5 elements and begins with 2,3
L ins 0..16, % the list elements are between 0 and 16
sum(L, #=, 16). % the sum of the list elements is 16

如果您查询此谓词,您会得到剩余目标作为答案,因为该谓词没有唯一的解决方案:

   ?- numbers(L).
L = [2,3,_A,_B,_C],
_A in 0..11,
_A+_B+_C#=11,
_B in 0..11,
_C in 0..11

将 label/1 添加到查询中,您将获得所有 78 个解决方案:

   ?- numbers(L), label(L).
L = [2,3,0,0,11] ? ;
L = [2,3,0,1,10] ? ;
L = [2,3,0,2,9] ? ;
...

您可以像这样使用 bagof/3 收集所有解决方案:

   ?- bagof(L,(numbers(L), label(L)),Xs).
Xs = [[2,3,0,0,11],[2,3,0,1,10],[2,3,0,2,9],[2,3,0,3,8],[2,3,0,4,7],[2,3,0,5,6],[2,3,0,6,5],[2,3,0,7,4],[2,3,0,8,3],[2,3,0,9,2],[2,3,0,10,1],[2,3,0,11,0],[2,3,1,0,10],[2,3,1,1,9],[2,3,1,2,8],[2,3,1,3,7],[2,3,1,4,6],[2,3,1,5,5],[2,3,1,6,4],[2,3,1,7,3],[2,3,1,8,2],[2,3,1,9,1],[2,3,1,10,0],[2,3,2,0,9],[2,3,2,1,8],[2,3,2,2,7],[2,3,2,3,6],[2,3,2,4,5],[2,3,2,5,4],[2,3,2,6,3],[2,3,2,7,2],[2,3,2,8,1],[2,3,2,9,0],[2,3,3,0,8],[2,3,3,1,7],[2,3,3,2,6],[2,3,3,3,5],[2,3,3,4,4],[2,3,3,5,3],[2,3,3,6,2],[2,3,3,7,1],[2,3,3,8,0],[2,3,4,0,7],[2,3,4,1,6],[2,3,4,2,5],[2,3,4,3,4],[2,3,4,4,3],[2,3,4,5,2],[2,3,4,6,1],[2,3,4,7,0],[2,3,5,0,6],[2,3,5,1,5],[2,3,5,2,4],[2,3,5,3,3],[2,3,5,4,2],[2,3,5,5,1],[2,3,5,6,0],[2,3,6,0,5],[2,3,6,1,4],[2,3,6,2,3],[2,3,6,3,2],[2,3,6,4,1],[2,3,6,5,0],[2,3,7,0,4],[2,3,7,1,3],[2,3,7,2,2],[2,3,7,3,1],[2,3,7,4,0],[2,3,8,0,3],[2,3,8,1,2],[2,3,8,2,1],[2,3,8,3,0],[2,3,9,0,2],[2,3,9,1,1],[2,3,9,2,0],[2,3,10,0,1],[2,3,10,1,0],[2,3,11,0,0]]

使用额外的目标长度/2,您可以计算收集到的解决方案的数量:

   ?- bagof(L,(numbers(L), label(L)),Xs), length(Xs,Len).
Len = 78,
Xs = [[2,3,0,0,11],[2,3,0,1,10],[2,3,0,2,9],[2,3,0,3,8],[2,3,0,4,7],[2,3,0,5,6],[2,3,0,6,5],[2,3,0,7,4],[2,3,0,8,3],[2,3,0,9,2],[2,3,0,10,1],[2,3,0,11,0],[2,3,1,0,10],[2,3,1,1,9],[2,3,1,2,8],[2,3,1,3,7],[2,3,1,4,6],[2,3,1,5,5],[2,3,1,6,4],[2,3,1,7,3],[2,3,1,8,2],[2,3,1,9,1],[2,3,1,10,0],[2,3,2,0,9],[2,3,2,1,8],[2,3,2,2,7],[2,3,2,3,6],[2,3,2,4,5],[2,3,2,5,4],[2,3,2,6,3],[2,3,2,7,2],[2,3,2,8,1],[2,3,2,9,0],[2,3,3,0,8],[2,3,3,1,7],[2,3,3,2,6],[2,3,3,3,5],[2,3,3,4,4],[2,3,3,5,3],[2,3,3,6,2],[2,3,3,7,1],[2,3,3,8,0],[2,3,4,0,7],[2,3,4,1,6],[2,3,4,2,5],[2,3,4,3,4],[2,3,4,4,3],[2,3,4,5,2],[2,3,4,6,1],[2,3,4,7,0],[2,3,5,0,6],[2,3,5,1,5],[2,3,5,2,4],[2,3,5,3,3],[2,3,5,4,2],[2,3,5,5,1],[2,3,5,6,0],[2,3,6,0,5],[2,3,6,1,4],[2,3,6,2,3],[2,3,6,3,2],[2,3,6,4,1],[2,3,6,5,0],[2,3,7,0,4],[2,3,7,1,3],[2,3,7,2,2],[2,3,7,3,1],[2,3,7,4,0],[2,3,8,0,3],[2,3,8,1,2],[2,3,8,2,1],[2,3,8,3,0],[2,3,9,0,2],[2,3,9,1,1],[2,3,9,2,0],[2,3,10,0,1],[2,3,10,1,0],[2,3,11,0,0]]

关于prolog - 列表总和必须为 16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44962136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com