C++ 代码:构造有趣的排列
#include
using namespace std;
void printInterestingPermutation(int n) { if (n <= 2) { cout << -1 << endl; return; }
vector<int> permutation;
for (int i = 1; i <= n; i++) {
permutation.push_back(i);
}
for (int i = 0; i < n - 1; i++) {
if (permutation[i] == i + 1) {
swap(permutation[i], permutation[i + 1]);
}
}
if (permutation[n - 1] == n) {
swap(permutation[n - 1], permutation[n - 2]);
}
for (int i = 0; i < n; i++) {
cout << permutation[i] << " ";
}
cout << endl;
}
int main() { int t; cin >> t;
while (t--) {
int n;
cin >> n;
printInterestingPermutation(n);
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pE47 著作权归作者所有。请勿转载和采集!