
100 - The 3n + 1 problem
Moderator: Board moderators
Repeated Compiler Error
My code compiles fine in my gcc..but the judge throws up compiler error always...I have tried MANY times..This is teh code..Any help would be appreciated. Thanks
[/code]
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;
}
Re: Repeated Compiler Error
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...
I couldn't help but comment on a few other things in your code viz:
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 */
{
[...]
}
3n+1 problem
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:
The Wrong Answer Code:
Can any one figure out the problem because i'm going mad ?
Thanks.
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:
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.
Thanks.
3n+1 problem
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:
The Wrong Answer Code:
Can any one figure out the problem because i'm going mad ?
Thanks.
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:
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.
Thanks.
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;
[pascal]p100
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.
>can anyone tell me where i am wrong?
thx
Last edited by gapsonhk on Fri Mar 24, 2006 5:48 am, edited 2 times in total.
Read this post
http://online-judge.uva.es/board/viewtopic.php?t=3015
http://online-judge.uva.es/board/viewtopic.php?t=3015
Solving problem is a no easy task...