A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.
Nickolas adores (爱慕喜欢)permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 ≤ i ≤ n) (n is the permutation size) the following equations hold ppi = i and pi ≠ i. Nickolas asks you to print any perfect permutation of size n for the given n.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
if(n%2==0)
{
for(int i=1; i<=n; i++)
{
if(i==1)
cout<<i+1;
else if(i%2)
cout<<" "<<i+1;
else
cout<<" "<<i-1;
}
}
else
cout<<-1<<endl;
return 0;
}
本文介绍了一种用于生成特定类型排列的算法,这类排列被称为“完美排列”。完美排列满足对于每一个位置i,其映射到的位置ppi等于i,且pi不等于i。文章通过示例代码展示了如何为给定大小n生成这样的排列,如果n为奇数,则不存在符合条件的完美排列。
4024

被折叠的 条评论
为什么被折叠?



