Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %первая задачка
- p(0,0):- !.
- p(X,N):- X<0, X2 is X*(-1), p(X2,N), !.
- p(X,N):- X>0, X1 is X // 10, p(X1,N1), N is N1 + (X mod 10).
- %вторая задачка
- to(X,Y,Z):- X<Y, Y1 is Y-X, Z1 is Z-1, to(X,Y1,Z1),!.
- to(X,Y,Z):- X>Y, X1 is X-Y, Z1 is Z-1, to(X1,Y,Z1),!.
- to(X,Y,Z):- X=Y, Z=1, !.
- %третья задачка
- ten(S,Ch):- Ch is S mod 10; Ch is S div 10.
- ost(S,S2,Ch,Z):- S=:=1+S2, Z=0.
- 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),!.
- ost(S,S2,Ch,Z):- Z=\=0,S<S2, \+ten(S,Ch), S3 is S+1, ost(S3,S2,Ch,Z),!.
- ost(S,S2,Ch,Z):- Z=0, S4 is S+1, \+ten(S4,Ch), ost(S4,S2,Ch,Z),!.
- %четвертая под б)
- contr(tree(Nod,Lef,Rig),N):- N=Nod ; contr(Lef,N) ; contr(Rig,N).
- con(tree(Nod,Lef,Rig),N):- var(N), contr(tree(Nod,Lef,Rig),N).
- con(tree(Nod,_,_),N):- nonvar(N), N=Nod, !.
- con(tree(Nod,_,Rig),N):- nonvar(N), N>Nod, \+Rig=nil, con(Rig,N),!.
- con(tree(Nod,Lef,_),N):- nonvar(N), N<Nod, \+Lef=nil, con(Lef,N),!.
- %четвертая под в)
- insert(nil,X,tree(X,nil,nil)):-!.
- insert(tree(H,L,R),X,Tree):- X=H,!, Tree=tree(H,L,R).
- insert(tree(H,L,R),X,Tree):- X<H, !, insert(L,X,Tree1), Tree=tree(H,Tree1,R).
- 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