一道pascal题

2025-05-18 13:12:55
推荐回答(4个)
回答(1):

var
a,b:array[0..9] of longint;
hash,ok:array[0..9] of boolean;
n,max,z:longint;

procedure doit1; //已知元素求顺序
var
i,j:longint;
begin
for i:=0 to max-1 do
read(a[i]); //读入max个元素
b[0]:=0; //第1个元素前面有0个比它大的数
for i:=1 to max-1 do //扫描第2到第max个元素
for j:=0 to i-1 do //扫描i前面i-1个数
if a[j] //若i前面有数比它小则顺序数加1
for i:=0 to max-2 do
write(b[i],' '); //输出,每个顺序数间1个空格
writeln(b[max-1]);
end;

procedure doit2; //已知顺序求元素
var
i,j,z,s:longint;
begin
for i:=0 to max-1 do //读入max个顺序数
read(b[i]);
for i:=1 to max do
begin
z:=0;
s:=0;
for j:=0 to max-1 do //从推出最大的元素到推出最小的元素
begin
if (b[j]=z) and (b[j]<>0) and hash[j] then
//它前面没推出过的数与它的顺序数相符,且顺序数不为0
begin
a[j]:=max-i; //算出元素大小
ok[i]:=true; //已找出第i大的数
if s<>0 then
//若前面也有类似的符合条件的数,则取后面的数
hash[s]:=true;
s:=j; //覆盖前面的类似的符合条件的数
hash[j]:=false; //此数已被推出过(可能被覆盖)
inc(z); //继续查找后面,有则被覆盖
end;
if hash[j] then inc(z);
//若这个数没推出过,则它前面的比它小的数多1个
end;
if not ok[i] then //从1到max找不到符合条件的数
begin
for j:=0 to max-1 do
if hash[j] then
//那么必是在前面(如 [4] 3 0 5 1 2),而且没被推出过(如 4 [3] 0 5 1 2)
begin
a[j]:=max-i; //找到最靠近前面的此类数
hash[j]:=false; //此数已被推出过
break;
end;
end;
end;
for i:=0 to max-2 do
write(a[i],' '); //输出,每个元素数间1个空格
writeln(a[max-1]);
end;

begin
fillchar(hash,sizeof(hash),true);
readln(n,max);
if n=0 then doit1; //判断计算方法
if n=1 then doit2;
end.

回答(2):

都好长啊,来个短的。
var n,i,j,dt,s:longint;f:boolean;
a,c:array[0..10000]of longint;
b:array[0..10000] of boolean;
begin
assign(input,'encode.in');reset(input);
assign(output,'encode.out');
rewrite(output);read(dt);
if dt=1 then begin readln(n);
for i:=1 to n do read(a[i]);
for i:=1 to n do
for j:=1 to i-1 do
if a[i]>a[j] then inc(c[i]);
for i:=1 to n-1 do
write(c[i],' ');writeln(c[n]);end else begin
fillchar(b,sizeof(b),true);read(n);
for i:=1 to n do read(a[i]);
s:=-1;j:=0;
for i:=n downto 1 do begin f:=true;
while f do begin
if b[j] then inc(s);
if s=a[i] then begin b[j]:=false;c[i]:=j;s:=-1;j:=-1;f:=false;end;
inc(j);end;end;
for i:=1 to n-1 do write(c[i],' ');writeln(c[n]);end;
close(input);close(output);
end.

回答(3):

var
a,b:array[0..9] of longint;
hash,ok:array[0..9] of boolean;
n,max,z:longint;

procedure doit1; //已知元素求顺序
var
i,j:longint;
begin
for i:=0 to max-1 do
read(a[i]); //读入max个元素
b[0]:=0; //第1个元素前面有0个比它大的数
for i:=1 to max-1 do //扫描第2到第max个元素
for j:=0 to i-1 do //扫描i前面i-1个数
if a[j] //若i前面有数比它小则顺序数加1
for i:=0 to max-2 do
write(b[i],' '); //输出,每个顺序数间1个空格
writeln(b[max-1]);
end;

procedure doit2; //已知顺序求元素
var
i,j,z,s:longint;
begin
for i:=0 to max-1 do //读入max个顺序数
read(b[i]);
for i:=1 to max do
begin
z:=0;
s:=0;
for j:=0 to max-1 do //从推出最大的元素到推出最小的元素
begin
if (b[j]=z) and (b[j]<>0) and hash[j] then
//它前面没推出过的数与它的顺序数相符,且顺序数不为0
begin
a[j]:=max-i; //算出元素大小
ok[i]:=true; //已找出第i大的数
if s<>0 then
//若前面也有类似的符合条件的数,则取后面的数
hash[s]:=true;
s:=j; //覆盖前面的类似的符合条件的数
hash[j]:=false; //此数已被推出过(可能被覆盖)
inc(z); //继续查找后面,有则被覆盖
end;
if hash[j] then inc(z);
//若这个数没推出过,则它前面的比它小的数多1个
end;
if not ok[i] then //从1到max找不到符合条件的数
begin
for j:=0 to max-1 do
if hash[j] then
//那么必是在前面(如 [4] 3 0 5 1 2),而且没被推出过(如 4 [3] 0 5 1 2)
begin
a[j]:=max-i; //找到最靠近前面的此类数
hash[j]:=false; //此数已被推出过
break;
end;
end;
end;
for i:=0 to max-2 do
write(a[i],' '); //输出,每个元素数间1个空格
writeln(a[max-1]);
end;

begin
fillchar(hash,sizeof(hash),true);
readln(n,max);

if n=1 then doit2;
end.

回答(4):

求编码的应该是最长上升序列吧,求好后照输就行了,第二个还不会