next_permutation用法

next_permutation用法:

next_permutation在#include<algorithm>头文件中。

next_permutation用法类似sort函数。

注意:使用前应该先排序再使用此函数!

1.int

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a[5] = { 1,2,4,3 };

sort(a,a+4);
do
{
for (int i = 0;i < 4;i++)
cout << a[i];
puts("");
} while (next_permutation(a,a+4));
return 0;
}

 

 

2. string,char都行

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int ans = 0;
string s = "dewb";
sort(s.begin(), s.end());
do
{
cout << s << endl;
} while (next_permutation(s.begin(),s.end()));
return 0;
}

 

 

 

 3.计算xxx是第几排列

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int ans = 0;
string s = "abcd";

sort(s.begin(), s.end());

do
{
ans++;
if (s == "acbd")
cout << ans;
} while (next_permutation(s.begin(),s.end()));
return 0;
}

 

posted @ 2021-04-14 23:03  20kmのshimakaze  阅读(183)  评论(0)    收藏  举报