SSL1132 编码问题

编码问题

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值