I think there should be an extra closing bracket. that isprintf(" %d\n",maxlength);
}
printf(" %d\n",maxlength);
}
}
Moderator: Board moderators
Code: Select all
#include<stdio.h>
void main()
{
long i,bi,min,max,temp;
int length,maxlength;
while(1)
{
printf("%ld %ld",min,max);
maxlength=1;
if(max<min)
{
temp=max;
max=min;
min=temp;
}
for(i=max;i>=min;i--)
{
length=1;
bi=i;
while(i!=1)
{
if(i%2==1)
{
length+=1;
i=i*3+1;
}
if(i%2==0)
{
length+=1;
i=i/2;
}
}
if(length>maxlength)
maxlength=length;
i=bi;
}
printf(" %d\n",maxlength);
}
}
Code: Select all
{ @JUDGE_ID: 37494NH 100 Pascal "The 3n + 1 problem" }
const
maxx=1000000;
var
i,j:longint;
n,k,max:longint;
l:extended;
was:array[1..maxx]of longint;
Procedure proces;
begin
max:=0;
for i:=n to k do
begin
l:=i;
j:=0;
while l<>1 do
begin
if (l<=maxx)and(was[trunc(l)]<>0) then
begin
j:=j+was[trunc(l)];
break;
end;
if l/2=trunc(l/2) then l:=l/2 else l:=l*3+1;
inc(j);
end;
was[i]:=j;
j:=j+1;
if j>max then max:=j;
end;
writeln(max);
end;
begin
while not eof do
begin
read(n,k);
write(n,' ',k,' ');
if n>k then
begin
j:=k;
k:=n;
n:=j;
end;
if n=0 then exit;
proces;
end;
end.
Code: Select all
01966610_24.c: In function `get_cycle_length':
01966610_24.c:9: parse error before `/'
01966610_24.c:11: parse error before `/'
Code: Select all
/* @JUDGE_ID: 37633FY 100 C */
#include <stdio.h>
int get_cycle_length(int n)
{
int cycle_length = 1;
while (n != 1)
{
if (n % 2 == 0) { // number is even
n = n / 2;
} else { // number is odd
n = (n * 3) + 1;
}
cycle_length++;
}
return cycle_length;
}
int get_max_cycle_length(int i, int j) {
int max_cycle_length = 0;
int cycle_length = 0;
int index = 0;
int max = 0;
if (i > j) {
max = i;
i = j;
j = max;
}
for (index = i; index <= j; index++) {
cycle_length = get_cycle_length(index);
if (cycle_length > max_cycle_length) {
max_cycle_length = cycle_length;
}
}
return max_cycle_length;
}
int main() {
int i = 0;
int j = 0;
while (scanf("%d %d", &i, &j)) {
printf("%d %d %d\n", i, j, get_max_cycle_length(i, j));
}
return 0;
}
In http://acm.uva.es/problemset/pascal.html it saysOmShanti wrote:I don't know what is wrong:[pascal]program program100;
const
Niesk=0;
var
tab: array[1..1000000] of qword;
i,j,k,h,p: longint;
max,printed: qword;
[/pascal]
Can that be the reason for the error?Do NOT use LongInt type!