有没有大佬能帮忙解释一下为什么我用next_permutation会有两遍相同的输出啊.
就是例如说输出1 2 3会有两个一样的。
很奇怪。
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
int a[21];
int main() {
int n, r;
cin >> n >> r;
for (int i = 1; i <= n; i++) {
a[i] = i;
}
do {
int c = 0;
for (int i = 1; i <= r - 1; i++) {
if (a[i] > a[i + 1]) {
c = 1;
break;
}
}
if (c == 0) {
for (int i = 1; i <= r; i++) {
cout << setw(3) << a[i];
}
cout << endl;
}
} while (next_permutation(a + 1, a + 1 + n));
return 0;
}