【普及模拟】Array

问题描述
Alice 有一个数列ai。
但是她不喜欢这个数列,于是她决定随机交换其中两个数。
Alice 想知道,交换后的数列与原数列相同的数有多少个。请求出所有可能的
值。
输入
第一行一个正整数n,表示Alice 的数列的长度。
第二行n 个正整数,第i 个数表示ai,即交换前的数列的第i 项。
输出
一行若干个严格递增的正整数,用空格隔开,表示交换后的数列与原数列相同的数的个数。
样例输入
32
3 3
样例输出
1 3
样例解释
交换后可能的数列有2 3 3, 3 2 3, 3 3 2,个数分别是3, 1, 1。
算法讨论
暴力即可。

const
  maxn=105;
var
  a,c:array[0..maxn] of longint;
  f:array[0..1000] of boolean;
  b:array[0..maxn*maxn] of longint;
  i,j,n,p:longint;

procedure qsort(l,r:longint);
var
  i,j,m:longint;
begin
  i:=l; j:=r;
  m:=b[(l+r) div 2];
  repeat
    while b[i]<m do
      inc(i);
    while b[j]>m do
      dec(j);
    if i<=j
      then begin
             b[0]:=b[i];
             b[i]:=b[j];
             b[j]:=b[0];
             inc(i); dec(j)
           end;
  until i>j;
  if l<j
    then qsort(l,j);
  if i>r
    then qsort(i,r)
end;

begin
  assign(input,'array.in'); reset(input);
  assign(output,'array.out'); rewrite(output);
  read(n);
  for i:=1 to n do
    read(a[i]);
  for i:=1 to n-1 do
    for j:=i+1 to n do
      begin
        c:=a;
        c[0]:=c[i]; c[i]:=c[j]; c[j]:=c[0];
        inc(p);
        b[p]:=n-2;
        if c[i]=a[i]
          then inc(b[p]);
        if c[j]=a[j]
          then inc(b[p])
      end;
  if p>1
    then qsort(1,p);
  for i:=1 to p do
    if (f[b[i]]) and (b[i]<>0)
      then continue
      else if b[i]=0
             then begin
                    write(0);
                    close(input); close(output);
                    halt
                  end
             else begin
                    write(b[i],' ');
                    f[b[i]]:=true
                  end;
  close(input); close(output)
end.

这里写图片描述
Pixiv ID:61325004

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值