SSL1132 编码问题

来源:互联网 发布:windows rt刷安卓 编辑:程序博客网 时间:2024/06/05 15:45

编码问题

Description

设有一个数组A:array [0..N-1] of integer;
存放的元素为0 - N-1(1A[j](i<>j)。例如当N=6时,有:A=(4,3,0,5,1,2)。此时,数组A的编码定义如下:A[0]编码为0,A[i]编码为:在A[0],A[1],…,A[i-1]中比A[i]的值小的个数i=1,2,…,N-1)
上面数组A的编码为:B=(0,0,0,3,1,2)
要求编程解决以下问题:
(1)给出数组A后,求出其编码;
(2)给出数组A的编码后,求出A中的原数据

Input

第一行为一个正整数q,表示要解决的第(q)问题
第二行为一个正整数n
第三行为n个整数

Output

第(1)问题输出"B = "和数组B中的数
第(2)问题输出"A = "和数组A中的数

Sample Input

1
8
1 0 3 2 5 6 7 4
Sample Output

B = 0 0 2 2 4 5 6 4

分析:第一问都会不说了,第二问从后往前做因为最后一个数总是确定的,自己想想为什么。。。

代码

const  maxn=10000;var  a,b:array[0..maxn] of longint;  f:array[0..maxn] of boolean;  i,j,n,q,p:longint;begin  readln(q);  readln(n);  for i:=1 to n do    read(a[i]);  if q=1    then begin           for i:=1 to n do             for j:=1 to i-1 do               if a[j]<a[i] then inc(b[i]);           write('B = ');         end    else begin           for i:=n downto 1 do             begin               p:=-1;               for j:=0 to n-1 do                 if not f[j] then                   begin                     inc(p);                     if a[i]=p then break;                      end;               b[i]:=j;               f[j]:=true;             end;           write('A = ');         end;  for i:=1 to n do    write(b[i],' ');end.