PASCAL 求数串的原始排列
题目:前N个自然数排成一串:X1,X2,X3.....Xn,先取出x1,将x2,x3移到数串尾,再取出x4,将x4,x6移到数串尾,.......类推直至取完.取出的序列...
题目:前N个自然数排成一串: X1,X2,X3.....Xn,先取出x1,将x2,x3移到数串尾,再取出x4,将x4,x6移到数串尾,....... 类推直至取完.
取出的序列恰好是:1,2,3......n.要求输入N,求原来的数串的排列方式. 展开
取出的序列恰好是:1,2,3......n.要求输入N,求原来的数串的排列方式. 展开
2010-03-13
展开全部
var
a,b:array[1..1000] of integer;
p,k,n,i,j,q:integer;
begin
write('input n=');
readln(n);
b[1]:=1;
for i:=2 to n-1 do a[i]:=i+1;
a[n]:=2;k:=1;p:=1;j:=n;q:=2;
repeat
j:=a[j];inc(k);
if k=3 then
begin
b[a[j]]:=q;
inc(p);
a[j]:=a[a[j]];k:=1;inc(q);
end;
until p=n;
for i:=1 to n do write(b[i]:4);
readln;
end.
a,b:array[1..1000] of integer;
p,k,n,i,j,q:integer;
begin
write('input n=');
readln(n);
b[1]:=1;
for i:=2 to n-1 do a[i]:=i+1;
a[n]:=2;k:=1;p:=1;j:=n;q:=2;
repeat
j:=a[j];inc(k);
if k=3 then
begin
b[a[j]]:=q;
inc(p);
a[j]:=a[a[j]];k:=1;inc(q);
end;
until p=n;
for i:=1 to n do write(b[i]:4);
readln;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询