作业帮 > 综合 > 作业

noip 用pascal语言设计

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/22 00:34:39
noip 用pascal语言设计
考虑如下序列的生成算法:从整数n开始,如果n是偶数,把他除以2;如果他是奇数,把他乘3加1.用新得到的值重复上面的步骤,知道n=1为止.例如n=22时,这个算法的生成序列是:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
人们猜想(没有证明)对于任意的整数n,该算法总能得到终止于n=1.这个猜想至少对于1000000以内的数都正确.
对于给定的n,该序列的元素(包括1)个数称之为n的循环节长度.例如上例中循环节长度为16.
输入格式 Input Format
输入两个数i,j,你的任务是计算i到j(包含i和j),之间的整数中,循环节长度最大的值
并且有多组数据!
0
program ex01;
var
a:array[1..100000000] of integer;
b,c:array[1..100] of longint;
i,n,m,x,y,j:longint;
procedure search(x,y,z:longint);
begin
if ( (y>1000000) or (a[y]0)) then
if odd(y) then search(x,y+y+y+1,z+1)
else search(x,y shr 1,z+1)
else a[x]:=a[y]+z;
end;
procedure f(x:longint);
begin
if a[x]0 then exit;
if odd(x) then begin if x+x+x+1>1000000 then search(x,x,0) else begin f(x+x+x+1);a[x]:=a[x+x+x+1]+1;end;end
else begin f(x shr 1);a[x]:=a[x shr 1]+1;end;
end;
begin
fillchar(a,sizeof(a),0);
read(n);
a[1]:=1;
a[2]:=2;
a[4]:=3;
for i:=1 to n do
read(b[i],c[i]);
for j:=1 to n do
begin
m:=0;
for i:=b[j] to c[j] do
begin
f(i);
if a[i]>m then m:=a[i];
end;
writeln(b[j],' ',c[j],' ',m);
end;
end.
不过你需要先输入有几组数据,不然的话你可以改动一下输入