Sicily. 全排序输出
2015-10-10 15:27
435 查看
输入一个数字n,输出从1~n ( 1 < n < 10 )组成的数字的全排列,每个排列占一行,输出按照数值升序排列
比如输入3,则输出是:
123
132
213
231
312
321
递归算法。转自这位大神
用<algorithm>的next_permutation的方法(没什么技术含量):
比如输入3,则输出是:
123
132
213
231
312
321
递归算法。转自这位大神
#include<iostream> #include<string> using namespace std; void permutation(string pre, string remain) { if (remain.size() == 1) { cout << pre << remain << endl; } else { for (int i = 0; i < remain.size(); i++) { string newPre = pre + remain[i]; string newRemain = remain; newRemain.erase(i, 1); permutation(newPre, newRemain); } } } int main() { int T,n ; cin >> T; while (T--) { cin >> n; string start = ""; for (char i = '1'; i < '1' + n; i++) { start += i; } permutation("", start); } return 0; }
用<algorithm>的next_permutation的方法(没什么技术含量):
#include<iostream> #include<string> #include<algorithm> using namespace std; int main() { int T,n ; cin >> T; while (T--) { cin >> n; string str = ""; for (char i = '1'; i < '1' + n; i++) { str += i; } do { cout << str << endl; } while (next_permutation(str.begin(), str.begin() + n)); } return 0; }
相关文章推荐
- nginx proxy超时报错 upstream timed out (110: Connec...
- 我的进阶曲线十二
- Deep Learning Papers
- 读《集中优势兵力,各个歼灭敌人》
- js将数字转换成中文
- 安卓发展史以及安卓和苹果对比PPT
- 银联SDK
- lintcode :二叉树的最大深度
- C++Primer第五版 练习11.25(解答)
- linux 内核与用户空间通信之netlink使用方法
- bootstrap属性
- [转载]jQuery学习之prop和attr的区别
- @Autowired使用中的问题及@Qualifire的使用
- 解决windows 下 node-webkit 不能播放声音的问题
- 我的进阶曲线十一
- mysql数据库的一些常用命令(一)
- jquery选择器:获取父级元素、同级元素、子元素
- Linux软件间的依赖关系(转)
- CGRectOffset与CGRectInset的计算公式
- 10.10 dos试验