Posted: Mon Mar 13, 2006 6:06 am
Got it! Thanks for the help 

Code: Select all
#include<stdio.h>
#include<stdlib.h>
//This function returns the 'cycle length' for n, as defined in the 3n+1 problem
unsigned long cycleLength(unsigned long n)
{
int len = 1;
while(n!=1)
{
if(n%2)
n=3*n + 1;
else
n=n/2;
++len;
}
return len;
}
//This prints the maximum cycle length of numbers between i and j inclusive
void maxCycleLength(unsigned long i, unsigned long j)
{
int swap_flag=0;
unsigned long k;
unsigned long len;
unsigned long maxLen=0;
if(i>j)
{
i=i^j;
j=i^j;
i=i^j;
swap_flag=1;
}
for(k=i; k<=j; k++)
if((len=cycleLength(k))>maxLen)
maxLen = len;
if(swap_flag)
printf("%ld\t%ld\t%ld\n", j,i,maxLen);
else
printf("%ld\t%ld\t%ld\n", i,j,maxLen);
}
int main(void)
{
unsigned long i;
unsigned long j;
while(scanf("%ld %ld",&i,&j)==2)
{
if(i<1 || j<1)
{
printf("\nInvalid Input");
return 1;
}
maxCycleLength(i,j);
}
return 0;
}
The judge's C compiler gets high on C++ comments.ausiva wrote:My code compiles fine in my gcc..but the judge throws up compiler error always...
Code: Select all
#include<stdio.h>
#include<stdlib.h>
//This function returns the 'cycle length' for n, as defined in the 3n+1
problem
/* Use comments such as this one */
unsigned long cycleLength(unsigned long n)
{
int len = 1;
[...]
return len;
/* types: declare len as an unsigned int */
}
//This prints the maximum cycle length of numbers between i and j inclusive
void maxCycleLength(unsigned long i, unsigned long j)
{
int swap_flag=0;
unsigned long k;
unsigned long len;
unsigned long maxLen=0;
if(i>j)
{
i=i^j;
j=i^j;
i=i^j;
/* you can get worse [oops, terse] :)
i ^= j ^= i ^= j;
*/
[...]
/* need %lu for unsigneds */
if(swap_flag)
printf("%ld\t%ld\t%ld\n", j,i,maxLen);
[...]
}
int main(void)
{
unsigned long i;
unsigned long j;
while(scanf("%ld %ld",&i,&j)==2)
/* need %lu for unsigneds */
{
[...]
}
Code: Select all
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.
Code: Select all
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.
Code: Select all
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.
Code: Select all
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.
Code: Select all
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;
Code: Select all
while n<>1 do
begin
if odd(n) then n:=(3*n)+1 else n:=(n div 2);
c:=c+1;
end;
if c>z then z:=c;
Code: Select all
program p100;
var n,i,j,k:longint;
c,max:integer;
begin
while not(eof())do begin
max:=0;
readln(i,j);
write(i,' ',j,' ');
if i>j then begin
k:=i;
i:=j;
j:=k;
end;
for k:=i to j do begin
n:=k;
c:=0;
if n=1 then
c:=0
else begin
repeat
if n mod 2=0 then
n:=n div 2
else
n:=3*n+1;
c:=c+1;
until n=1;
end;
if c>=max then
max:=c+1;
end;
writeln(max);
end;
end.