next_permutation与使用
来源:互联网 发布:三菱中性笔 知乎 编辑:程序博客网 时间:2024/06/03 19:01
现在有一道很简单的题目,那就是输出1——n的所有排列数!
比如说,输入n=5;那么要求求出1,2,3,4,5这几个数所有的排列数!
c++中有一个next_permutation函数,它包含在algorithm头文件中,可以方便的求出所有的排列
数,可是你知道它是怎么实现的么?
对那个函数进行了简单的模拟,模拟函数如下:
// next_permutation(begin(),end()+1)存在返回正数,否则返回0 // prev_permutation(begin(),end()+1)#include<cstdio>#include<algorithm>using namespace std;int main(){int n;int num[50];scanf ("%d",&n);for (int i = 1 ; i <= n ; i++){num[i] = i;}do{for (int i = 1 ; i <= n ; i++)printf ("%d%c",num[i],i == n ? '\n' : ' ');}while(next_permutation(num+1,num+1+n));return 0;}
#include <cstdio>#include <algorithm>#include <cstring>#define MAX 100 using namespace std; int main(){ int length; char str[MAX]; gets(str); length = strlen(str); sort(str, str + length); puts(str); while (next_permutation(str, str + length)) { puts(str); } return 0;}
阅读全文
0 0
- next_permutation与使用
- 【C++ STL应用与实现】62: 如何使用std::next_permutation
- 字典序与next_permutation
- next_permutation与prev_permutation
- 全排列与next_permutation
- next_permutation 的使用
- next_permutation的使用
- 使用stl的next_permutation
- next_permutation的使用
- next_permutation函数的使用
- next_permutation 的使用
- STL next_permutation与prev_mutation解析
- next_permutation用法 与 字典序
- next_permutation()的用法,与作用
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- ViewPager+Fragment 懒加载学习笔记(二)
- 191. Number of 1 Bits
- Windows性能监控工具之Perfmon
- spring学习笔记6--AOP
- JAVA NIO(一):Buffer.mark()的用法
- next_permutation与使用
- Java之JFileChooser设置窗体打开路径
- php 注册email激活账号技术
- Codeforces 4A
- 【Android 从入门到放弃】 ButterKnife 使用
- C++实现顺序表
- 正则表达式
- 产品分析和深度学习资料(持续补充)
- JAVA NIO(二):小端字节顺序