NOJ1103&next_permutation()
来源:互联网 发布:百合网数据库 编辑:程序博客网 时间:2024/06/04 22:38
描述
全排列的生成就是对于给定的字符集或数集,用有效的方法将所有可能的全排列无重复无遗漏地枚举出来。对给定的字符集中的字符规定一个先后关系,在此基础上规定两个全排列的先后是从左到右逐个比较对应的字符的先后,或根据给定的数集中的大小关系,规定两个全排列的先后是从左到右逐个比较对应的数的大小,即依照字典序给出全排列。例如字符集{1,2,3},较小的数字较先,这样按字典序生成的全排列是:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
给定n个整数,现请编程求它们所有的全排列。
输入
输入包括两个行,第一行给出正整数n( 0 < n <=8), 第二个是 n个整数(大小范围:[-10^4, 10^4])。
输出
按字典序输出这n个整数的全排列,每一行给出一个全排列。
样例输入
3
1 23 88
样例输出
1 23 88
1 88 23
23 1 88
23 88 1
88 1 23
全排列的生成就是对于给定的字符集或数集,用有效的方法将所有可能的全排列无重复无遗漏地枚举出来。对给定的字符集中的字符规定一个先后关系,在此基础上规定两个全排列的先后是从左到右逐个比较对应的字符的先后,或根据给定的数集中的大小关系,规定两个全排列的先后是从左到右逐个比较对应的数的大小,即依照字典序给出全排列。例如字符集{1,2,3},较小的数字较先,这样按字典序生成的全排列是:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
给定n个整数,现请编程求它们所有的全排列。
输入
输入包括两个行,第一行给出正整数n( 0 < n <=8), 第二个是 n个整数(大小范围:[-10^4, 10^4])。
输出
按字典序输出这n个整数的全排列,每一行给出一个全排列。
样例输入
3
1 23 88
样例输出
1 23 88
1 88 23
23 1 88
23 88 1
88 1 23
88 23 1
C++中有专门用于全排列的next_permutation()函数,当其执行次数小于全排列次数时返回true,全排列结束后返回false。有两个参数a.begin()和a.end()。
#include <iostream>#include <deque>#include <algorithm>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int n,tmp;cin>>n;deque<int> deq;while(n--){cin>>tmp;deq.push_back(tmp);}sort(deq.begin(),deq.end());do{for(int i=0;i<deq.size();++i){if(i!=0)cout<<" ";cout<<deq[i];}cout<<endl;}while(next_permutation(deq.begin(),deq.end()));return 0;}
0 0
- NOJ1103&next_permutation()
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- : java.lang.SecurityException: Unable to find app for caller android.app.ApplicationThreadProxy 解决方法
- HDU 2544 最短路 【Dijkstra算法】
- 为大家编写一个数据可视化的软件
- docker use case
- EnhanceIO SSD Caching
- NOJ1103&next_permutation()
- MFC多线程编码--用户界面线程(四)
- 字节序与大小端
- 判断是否为64位机
- 穿越麦田的小路
- LBS预案作战指挥系统应用
- 设计模式——备忘录模式
- OpenCV的java绑定显示图片
- C/C++常用预定宏