hi programmers.
i have two solutions for acm 3n+1 problem#100 one works and the other don't.
i have tested both of them they worked for me and give the same answer but acm accepted one an doesn't accept the other.
The Accepted Code:
program p100 (input, output);
var
i, j: integer;
function getCL(N: integer): integer;
var k: integer;
begin
k := 1;
while N <> 1 do begin
if odd(N) then N := 3*N + 1
else N := N div 2;
k := k + 1;
end;
getCL := k;
end;
function getMaxCL(i, j: integer): integer;
var k: integer;
max, curCL: integer;
begin
max := 0;
for k:=i to j do begin
curCL := getCL(k);
if curCL > max then max := curCL;
end;
getMaxCL := max;
end;
begin
while not eof(input) do begin
readln(i, j);
write(i, ' ', j, ' ');
if i < j then
writeln(getMaxCL(i, j))
else
writeln(getMaxCL(j, i));
end;
end.
The Wrong Answer Code:
program p100 (input, output);
var
n,i,c,j,f,z:integer;
begin
while not eof(input) do
begin
readln(i, j);
write(i, ' ', j,' ');
z:=0;
if j>i then
begin
for f:=i to j do
begin
c:=1;
n:=f;
while n<>1 do
begin
if odd(n) then n:=(3*n)+1 else n:=(n div 2);
c:=c+1;
if c>z then z:=c;
end;
end;
end
else
begin
for f:=j to i do
begin
c:=1;
n:=f;
while n<>1 do
begin
if odd(n) then n:=(3*n)+1 else n:=(n div 2);
c:=c+1;
if c>z then z:=c;
end;
end;
end;
writeln(z);
end;
end.
Can any one figure out the problem because i'm going mad ?
Thanks[/code]