全排列函数next_permutation与prev_permutation
来源:互联网 发布:华为查看端口 编辑:程序博客网 时间:2024/05/17 20:00
C++ 全排列函数 next_permutation与prev_permutation
C++ STL中提供了next_permutation与prev_permutation可以获取数字或者是字符的全排列,其中next_permutation提供升序、prev_permutation提供降序。
1.next_permutation(first,last)
说明:对[first,last)区间进行排列,第一次输出当前的排列顺序,例如:1234,第二次输出比第一次大的下一种排列,则为1243(若为数组,则通过数字比较来判断大小,若为字符串,则通过ASCII码来比较大小)。
#include <iostream>#include <algorithm>using namespace std;int main(){ int a[100]; int n=0,i; while(cin>>a[n]) n++; sort(a,a+n); //输出升序的全排列则应将最初的数组排为最小顺序 //如1 4 3 2 ,则应先排为1 2 3 4 使next_premutation() //正常输出 do { for(i=0;i<n-1;i++) cout<<a[i]; cout<<a[i]<<endl; }while(next_permutation(a,a+n)); return 0;}
2.next_permutation( first , last , cmp)
说明:我们可以通过改变cmp()函数来改变next_permutation()函数产生的到底是升序全排列还是,降序全排列。在不写cmp()函数时,next_permutation()默认为升序全排列。
#include <iostream>#include <algorithm>using namespace std;bool cmp(int a,int b){ return a>b;//a>b为降序,a<b为升序。}int main(){ int a[100]; int n=0,i; while(cin>>a[n]) { n++; } sort(a,a+n,cmp);//sort()也应该为降序。 do { for(i=0;i<n-1;i++) cout<<a[i]; cout<<a[i]<<endl; }while(next_permutation(a,a+n,cmp));//输出降序全排列 return 0;}
3.prev_permutation( first , last )与prev_permutation( first , last , cmp)的用法与前两项相同,这里就不过多赘述。
4.用next_permutation()对字符串进行排序。
#include <iostream>#include <algorithm>#include <cstring>using namespace std;int main(){ string str; cin>>str; sort(str.begin(),str.end()); do { cout<<str<<endl; }while(next_permutation(str.begin(),str.end()));//原理相同。 return 0;}
4.题目:字符的全排列(顺序:’A’<’a’<’B’<’b’<…<’Z’<’z’)。
#include <iostream>#include <algorithm>#include <string.h>using namespace std;int val(char c) // 按照'A'<'a'<'B'<'b'<...<'Z'<'z'的顺序,每个字母赋一个固定的权值。{ if(c >= 'A' && c <= 'Z') return 2 * (c - 'A'); else return 2 * (c - 'a') + 1;}bool cmp(char a, char b){ return val(a) < val(b);}int main(){ char str[100]; cin >> str; int n = strlen(str); sort(str, str + n, cmp); do { cout << str << endl; } while(next_permutation(str, str + n, cmp)); return 0;}
阅读全文
0 0
- 全排列函数next_permutation与prev_permutation
- 全排列函数next_permutation,prev_permutation
- C++ 全排列函数 std::next_permutation与std::prev_permutation
- stl全排列next_permutation()与prev_permutation()函数用法
- 全排列函数 : next_permutation and prev_permutation
- 全排列函数——next_permutation() & prev_permutation()
- STL全排列函数 next_permutation prev_permutation 及拓展
- next_permutation 与 prev_permutation的用法 (全排列)
- STL之生成全排列:next_permutation & prev_permutation
- STL全排列算法next_permutation和prev_permutation
- 排列函数 prev_permutation和next_permutation的使用
- C++ 求排列函数 next_permutation, prev_permutation
- 全排列 next_permutation()函数
- 全排列与next_permutation
- next_permutation与prev_permutation
- 全排列函数 next_permutation()
- 【全排列】next_permutation函数 【algorithm】
- next_permutation函数(全排列)
- 关于unity打android工程时的坑
- CMAKE模板
- Android图片加载之Fresco浅析
- spring boot使用jpa的@Modify的clearAutomatically=true的作用
- ACM错误提示/错误原因
- 全排列函数next_permutation与prev_permutation
- 梯度下降优化算法综述(转载)
- mybatis自动生成代码
- Python中用Keras构建LSTM模型进行时间序列预测
- Android空格占位符
- 欢迎使用CSDN-markdown编辑器
- spring依赖注入配置及简写形式
- 集群调度系统SGE的安装和配置
- windo