201711月04日普及组 Array

Description

Alice 有一个数列 ai 。
但是她不喜欢这个数列,于是她决定随机交换其中两个数。
Alice 想知道,交换后的数列与原数列相同的数有多少个。请求出所有可能的值。

Input

第一行一个正整数 n,表示 Alice 的数列的长度。
第二行 n 个正整数,第 i 个数表示 ai ,即交换前 的数列的第 i 项。

Output

一行若干个严格递增的 正整数,用空格隔开,表示交换后的数列与原数列相同的数的个数。

Sample Input

3
2 3 3
Sample Output

1 3

交换后可能的数列有 2 3 3, 3 2 3, 3 3 2,个数分别是 3, 1, 1。
Hint

对于 100% 的数据,1 ≤ n ≤ 100,1 ≤ a i ≤ 1000。

分析
对于任意数列,答案最多只有两个。
每次交换都有两种情况

第一种情况:交换的两个数相等。交换后,该数列等于原数列,答案为n。

第二种情况:交换的两个数不相等。交换后,则只有2个数不等,答案为n-2。

程序:

var
a:array[0..100] of longint;
f:array[0..100] of boolean;
i,j,n:longint;

begin
    assign(input,'array.in');
    reset(input);
    assign(output,'array.out');
    rewrite(output);
    readln(n);
    for i:=1 to n do
    read(a[i]);
    for i:=1 to n do
    for j:=i+1 to n do
    if a[i]=a[j] then f[n]:=true else f[n-2]:=true;
    for i:=0 to n do
    if f[i] then write(i,' ');
    close(input);
    close(output);
end.
posted @ 2017-11-04 15:32  银叶草  阅读(207)  评论(0编辑  收藏  举报
Live2D