Advertisement
PonaFly

Untitled

Sep 21st, 2019
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.25 KB | None | 0 0
  1. %первая задачка
  2. p(0,0):- !.
  3. p(X,N):- X<0, X2 is X*(-1), p(X2,N), !.
  4. p(X,N):- X>0, X1 is X // 10, p(X1,N1), N is N1 + (X mod 10).
  5.  
  6. %вторая задачка
  7. to(X,Y,Z):- X<Y, Y1 is Y-X, Z1 is Z-1, to(X,Y1,Z1),!.
  8. to(X,Y,Z):- X>Y, X1 is X-Y, Z1 is Z-1, to(X1,Y,Z1),!.
  9. to(X,Y,Z):- X=Y, Z=1, !.
  10.  
  11. %третья задачка
  12. ten(S,Ch):- Ch is S mod 10; Ch is S div 10.
  13. ost(S,S2,Ch,Z):- S=:=1+S2, Z=0.
  14. ost(S,S2,Ch,Z):- Z=\=0, S<S2, ten(S,Ch), S1 is S+1, Z1 is Z-1, ost(S1,S2,Ch,Z1),!.
  15. ost(S,S2,Ch,Z):- Z=\=0,S<S2, \+ten(S,Ch), S3 is S+1, ost(S3,S2,Ch,Z),!.
  16. ost(S,S2,Ch,Z):- Z=0, S4 is S+1, \+ten(S4,Ch), ost(S4,S2,Ch,Z),!.
  17.  
  18.  
  19. %четвертая под б)
  20. contr(tree(Nod,Lef,Rig),N):- N=Nod ; contr(Lef,N) ; contr(Rig,N).
  21. con(tree(Nod,Lef,Rig),N):- var(N), contr(tree(Nod,Lef,Rig),N).
  22. con(tree(Nod,_,_),N):- nonvar(N), N=Nod, !.
  23. con(tree(Nod,_,Rig),N):- nonvar(N), N>Nod, \+Rig=nil, con(Rig,N),!.
  24. con(tree(Nod,Lef,_),N):- nonvar(N), N<Nod, \+Lef=nil, con(Lef,N),!.
  25.  
  26. %четвертая под в)
  27. insert(nil,X,tree(X,nil,nil)):-!.
  28. insert(tree(H,L,R),X,Tree):- X=H,!, Tree=tree(H,L,R).
  29. insert(tree(H,L,R),X,Tree):- X<H, !, insert(L,X,Tree1), Tree=tree(H,Tree1,R).
  30. insert(tree(H,L,R),X,Tree):- X>H, !, insert(R,X,Tree1), Tree=tree(H,L,Tree1).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement