POJ 1207(3N+1)

内容目录

3n+1问题 果断暴力

Program P1207;
var
   i,j,k,n,m,ans:longint;
function max(a,b:longint):longint;
begin
   if a>b then exit(a) else exit(b);
end;
procedure swap(var a,b:longint);
var
   p:longint;
begin
   p:=a;
   a:=b;
   b:=p;
end;
begin
   while not eof do
   begin
      readln(n,m);
      write(n,' ',m,' ');
      if n>m then swap(n,m);
      ans:=0;
      for i:=n to m do
      begin
         j:=1;
         k:=i;
         while (k<>1) do
         begin
            if (k mod 2=0) then k:=k div 2
            else k:=k*3+1;
            inc(j);
         end;

         ans:=max(ans,j);
      end;
      writeln(ans);
   end;
end.