1

我必须填写一个长度为 n 位的列表。我知道 n-1 在 1 到 9 的范围内,一个数字可以在 1 到 99 的范围内。我是这样做的:

generate([First|Next],Czynniki):-
    between(1,99,First),
    generate2(Next).

generate2(Next):-
    sublist([1,2,3,4,5,6,7,8,9],Next).

sublist([],[]).
sublist([H|T],[H|S]):-
    sublist(T,S).
sublist([_|T],S):-
    sublist(T,S).

这样做我会生成一些相同的解决方案。也许你有一些想法,我怎样才能生成不重复的列表?

编辑

为了清楚起见,我(@repeat)添加了 OP 的以下相关评论:

在入口处,我有N未定义变量的长度列表。并想填写我的清单:N-1区间中的数字1-9和范围中的一个数字1-99

示例:N=5, L=[56,2,3,4,8] ...

4

3 回答 3

1

使用

:- use_module(library(clpfd)).

让我们digits10plusdigit100_n/2这样定义:

digits10plusdigit100_n(Zs,N) :-
   Zs = [CentDigit|DecDigits],
   length(Zs,N),
   CentDigit in  1..99,
   DecDigits ins 1..9,
   labeling([],Zs).

示例查询:

?- digits10plusdigit100_n(Zs,1).
  Zs = [1]
; Zs = [2]
; Zs = [3]
...
; Zs = [98]
; Zs = [99]
; false.

?- digits10plusdigit100_n(Zs,3).
  Zs = [1,1,1]
; Zs = [1,1,2]
; Zs = [1,1,3]
...
; Zs = [1,2,1]
; Zs = [1,2,2]
...
; Zs = [1,9,8]
; Zs = [1,9,9]
; Zs = [2,1,1]
; Zs = [2,1,2]
...
; Zs = [2,1,3]
; Zs = [2,1,4]
...
; Zs = [98,9,9]
; Zs = [99,1,1]
; Zs = [99,1,2]
...
; Zs = [99,9,8]
; Zs = [99,9,9]
; false.
于 2015-08-19T19:43:03.350 回答
0

可能会改变between(10,99,X) 你的谓词,生成小于 10 的数字,然后生成最后一个大于 10 的变量

于 2012-04-06T19:14:39.320 回答
0

这不只是@false 在这里非常优雅地所做的一种变体吗?

gen(Xs) :-
       between(1, 9, L),
       length(Xs, L),
       maplist(between(1,99), Xs).

?- gen(Xs).
Xs = [1] ;
Xs = [2] ;
Xs = [3] ;
Xs = [4] ;
Xs = [5] ;
  ..
Xs = [99] ;
Xs = [1, 1] ;
Xs = [1, 2] ;
Xs = [1, 3] ;
Xs = [1, 4] ;
..
Xs = [1, 98] ;
Xs = [1, 99] ;
Xs = [2, 1] ;
Xs = [2, 2] ;
Xs = [2, 3] ;
Xs = [2, 4] ;
Xs = [2, 5] ;
Xs = [2, 6] ;
于 2012-04-06T22:57:54.270 回答