Вводятся n чисел, определить наибольшую разницу между двумя соседними элементами
max_diff(CurrNum,CurrItem,MaxDiff,NumMaxDiff):-
NewNum is CurrNum+1,
write('['),write(NewNum),write('] > '),
read(NewItem),
( integer(NewItem),!,
Diff is abs(NewItem-CurrItem),
( Diff>MaxDiff, max_diff(NewNum,NewItem,Diff,NewNum);
Diff=
);
write('The maximal difference is '), write(MaxDiff),
Prev is NumMaxDiff-1,
write('\nbetween items with indexes ['), write(Prev),write('] and ['), write(NumMaxDiff), write(']\n')
).
goal:-
write('[1] > '),
read(Item),
integer(Item),!,
max_diff(1,Item,0,1).
(признаком окончания ввода чисел будем считать ввод любого нечислового атома)
Проверка:
?- goal.
[1] > 20.
[2] > 21.
[3] > 23.
[4] > 25.
[5] > 35.
[6] > 33.
[7] > end.
The maximal difference is 10
between items with indexes [4] and [5]
Yes
?-
|