next_permutation(排列问题)
2017-08-02 22:12
260 查看
全排列的模板
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort
int main () {
int myints[] = {1, 2, 3};
std::sort(myints, myints + 3);
std::cout << "The 3! possible permutations with 3 elements:\n";
do {
std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
} while ( std::next_permutation(myints, myints + 3) );
std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
return 0;
}
计算n个数的第k个排列
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[1010];
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++) a[i]=i+1;
sort(a,a+n);
int i=0;
while(next_permutation(a,a+n)){
i++;
if(i==k-1) break;
}
for(int i=0;i<n-1;i++) cout<<a[i]<<" ";
cout<<a[n-1]<<endl;
return 0;
}
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort
int main () {
int myints[] = {1, 2, 3};
std::sort(myints, myints + 3);
std::cout << "The 3! possible permutations with 3 elements:\n";
do {
std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
} while ( std::next_permutation(myints, myints + 3) );
std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
return 0;
}
计算n个数的第k个排列
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[1010];
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++) a[i]=i+1;
sort(a,a+n);
int i=0;
while(next_permutation(a,a+n)){
i++;
if(i==k-1) break;
}
for(int i=0;i<n-1;i++) cout<<a[i]<<" ";
cout<<a[n-1]<<endl;
return 0;
}
相关文章推荐
- 排列问题算法-next_permutation,康托编码
- STL里面的next_permutation求字典序排列问题
- STL中用next_permutation解决排列问题
- next_permutation解决排列问题的利器
- 7.2.1 生成1~n的排列(全排列)【STL__next_permutation()_的应用】
- 擅长排列的小明(C++next_permutation)
- next_permutation生成全排列打表法解决问题
- 全排列 next_permutation()函数
- [POJ1833] 排列(STL之next_permutation)
- (next_permutation) 排列2 hdu 1716
- STL全排列(next_permutation(str,str+length))and Java全排列
- Smallest Difference(Poj2718)(枚举全排列next_Permutation)
- STL源码——排列生成算法(next-permutation、pre-permutation)
- NYOJ 19 擅长排列的小名 next_permutation()的用法
- 排列问题 permutation(似乎原题是SRM 592 Little Elephant And Permutation Div1?)
- POJ1833 排列(next_permutation()的应用)
- C++STL中全排列函数next_permutation的使用
- STL经典算法集锦<六>之排列(next_permutation/prev_permutation)
- C++STL中全排列函数next_permutation的使用
- NYOJ 19 擅长排列的小明 (STL之next_permutation()的使用)