Advertisement
tuki2501

6174

Sep 7th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.69 KB | None | 0 0
  1. uses crt;
  2. var
  3.   st:string;
  4.   n:longint;
  5.  
  6. function num(x:char):longint;
  7. begin
  8.   num:=ord(x)-48;
  9. end;
  10.  
  11. procedure swap(var x,y:char);
  12. var tmp:char;
  13. begin
  14.   tmp:=x; x:=y; y:=tmp;
  15. end;
  16.  
  17. function sort(st:string;bool:boolean):longint;
  18. var i,j:longint;
  19. begin
  20.   for i:=1 to n do
  21.     for j:=i+1 to n do
  22.       if (num(st[i]) > num(st[j]) = bool) then
  23.         swap(st[i],st[j]);
  24.   val(st,sort);
  25. end;
  26.  
  27. procedure solve();
  28. var sub,c:longint;
  29. begin
  30.   c:=0;
  31.   readln(st);
  32.   n:=length(st);
  33.   repeat
  34.     begin
  35.       sub:=sort(st,false)-sort(st,true);
  36.       str(sub,st);
  37.       inc(c);
  38.     end;
  39.   until (sub=6174);
  40.   writeln(c);
  41. end;
  42.  
  43. begin
  44.   clrscr();
  45.   solve();
  46.   readln();
  47. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement