本文介绍了PROLOG 打印以 7 结尾且其数字之和大于 100 的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个接收数字列表的谓词,并仅打印以 7 结尾且其数字之和大于 100 的数字
I need to make a predicate that receives a numeric list and print only the numbers that end in 7 and that the sum of its digits is greater than 100
我为分离做了谓词,但我需要帮助将两个谓词合并,我的意思是这两个谓词进入一个唯一的谓词,这是我到目前为止所做的:
I made the predicates for separated but I need help making a union of the two predicates, I mean that the two predicates go into one only predicate, this is what I did so far:
%sum of digits greater than 100
multi(X):-
0 is X mod 100
sum([],0).
sum([P|Q],Z).
multi(P), sum(Q,Z1), Z is P + Z1.
sum([P|Q],Z).
not multi(P), sum(Q,Z).
%print the numbers that end in 7
end(Y):-
7 is Y mod 10.
listend([],0).
listend([P|Q]):-
end(P),write(P), nl, listend(Q).
listend([P|Q]):-
not(end(P)), listend(Q).
推荐答案
这对我有用:
?- filter([147, 24, 57, 17, 3667], X), write(X), nl, fail.
sumdigits(0, 0).
sumdigits(X, Z) :-
X > 0,
Z1 is X mod 10,
X2 is X // 10,
sumdigits(X2, Z2),
Z is Z1 + Z2.
filter([], []).
filter([H|X], [H|Y]) :-
sumdigits(H, D),
D > 10,
7 is H mod 10, !,
filter(X, Y).
filter([_|X], Y) :- filter(X, Y).
我明白了:
[147, 57, 3667]
No.
我假设你的意思是数字的总和大于 10,而不是 100.
I assumed you meant that the sum of the digits was greater than 10, rather than 100.
这篇关于PROLOG 打印以 7 结尾且其数字之和大于 100 的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!