生成全排列示例
来源:互联网 发布:unity3d 镜面反射 编辑:程序博客网 时间:2024/05/16 15:08
代码示例
#include <iostream>#include <string>using namespace std;void fun1(string & list,int n , int index){ if (index >= n) { cout << list << endl; } else { for (int i = index; i < n; i++) { swap(list[i] , list[index]); fun1(list , n , index + 1); swap(list[i] , list[index]); } }}void fun2(string & list , int n){ if (n == 1) { cout << list << endl; } else { for (int i = 1; i <= n; i++) { fun2(list , n - 1); if (n % 2 == 1) { swap(list[0] , list[n - 1]); } else { swap(list[i - 1] , list[n - 1]); } } }}void fun3(string & str){ int left = 0; int right = 0; for (int i = str.length() - 1; i > 0; i--) { if(str[i - 1] < str[i]) { left = i - 1; break; } if(i == 1) return; } for (i = str.length() - 1; i > left; i--) { if(str[i] > str[left]) { swap(str[left] , str[i]); break; } } left++; right = str.length() - 1; while(left < right) { swap(str[left++] , str[right--]); }}int factorial(int n){ return n == 1 ? n : n * factorial(n - 1);}int main(){ //string list("123456"); //fun1(list.length() , 0); //fun2(list.length()); string str("123456"); cout << str << endl; for (int i = 1; i < factorial(str.length()); i++) { fun3(str); //按照字典序生成下一个排列 cout << str << endl; } return 0;}
1.找到使得
2.找到使得
3.交换
4.将
0 0
- 生成全排列示例
- 全排列生成
- 生成全排列
- 生成全排列
- 生成全排列
- 全排列生成算法
- 全排列的生成
- 生成全排列
- 全排列的生成
- 如何生成全排列
- 全排列生成算法 .
- 生成全排列
- 全排列生成算法
- 全排列生成 Permutations
- 全排列生成算法
- 全排列生成
- 全排列生成算法
- 生成全排列
- ahoi2005 lane 航线规划
- 226. Invert Binary Tree 翻转二叉树
- 17. Letter Combinations of a Phone Number
- 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
- 运动目标跟踪(一)--搜索算法预测模型之KF,EKF,UKF
- 生成全排列示例
- Android UI之switch的thumb与track属性定制自己的switch
- FtpGetFile函数下载不到最新文件Solution
- 运动目标跟踪(二)--搜索算法预测模型之粒子滤波
- springboot-aop访问拦截
- 类——类的复用(组合、继承、代理)
- Android 观察者与被观察者
- 错误处理页例子
- iOS 进度框(一) MBProgressHUD