C++全排列
1.函数next_permutation
#include <algorithm>
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};//要先排好顺序do{
for (int j = 0; j < 9; ++j) {
cout << a[j] << " ";
}
cout << endl;
}while (next_permutation(a,a+9));
2.手写函数
void f(int k) {
for (int i = k; i < 9; ++i) {
{int t = a[i];a[i] = a[k];a[k] = t;}
for (int j = 0; j < 9; ++j) {
cout << a[j] << " ";
}
cout << endl;
f(k + 1);
{int t = a[i];a[i] = a[k];a[k] = t;}
}
}
本文作者:HDAWN
版权声明:本文采用
CC BY-NC-SA 3.0 CN协议进行许可