next_permutation的简单用法及例题
2017-04-07 18:00
405 查看
一:基本用法;
二:例题分析:
51nod 1384
全排列
给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = "1312",
输出为:
1123 1132 1213 1231 1312 1321 2113 2131 2311 3112 3121 3211
Input
Output
Input示例
Output示例
思路:不用指针便会TLE,惊呆我了!!!
#include<cstdio>
#include<cstring>
#include<algorithm>
#define max_n 1010
using namespace std;
char a[max_n];
int main()
{
scanf("%s",a);
int n=strlen(a);
sort(a,a+n);
char *p1,*p2; //不用指针会TLE
p1=&a[0];
p2=&a
;
do
{
printf("%s\n",a);
}while(next_permutation(p1,p2));
return 0;
}
#include<cstdio> #include<cstring> #include<algorithm> #define max_n 1010 using namespace std; int a[max_n]; int main() { int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); do { for(int i=0;i<n;i++) { if(i>0) printf(" "); printf("%d",a[i]); } printf("\n"); }while(next_permutation(a,a+n)); } return 0; }
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #define max_n 1010 using namespace std; vector<int> v; bool cmp(int x,int y) { return x<y; } int main() { int n,k; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { scanf("%d",&k); v.push_back(k); } sort(v.begin(),v.end(),cmp); do { for(int i=0;i<n;i++) { if(i>0) printf(" "); printf("%d",v.at(i)); } printf("\n"); }while(next_permutation(v.begin(),v.end())); } return 0; }
二:例题分析:
51nod 1384
全排列
给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = "1312",
输出为:
1123 1132 1213 1231 1312 1321 2113 2131 2311 3112 3121 3211
Input
输入一个字符串S(S的长度 <= 9,且只包括0 - 9的阿拉伯数字)
Output
输出S所包含的字符组成的所有排列
Input示例
1312
Output示例
1123 1132 1213 1231 1312 1321 2113 2131 2311 3112 3121 3211
思路:不用指针便会TLE,惊呆我了!!!
#include<cstdio>
#include<cstring>
#include<algorithm>
#define max_n 1010
using namespace std;
char a[max_n];
int main()
{
scanf("%s",a);
int n=strlen(a);
sort(a,a+n);
char *p1,*p2; //不用指针会TLE
p1=&a[0];
p2=&a
;
do
{
printf("%s\n",a);
}while(next_permutation(p1,p2));
return 0;
}
#include<cstdio> #include<algorithm> using namespace std; int a[110]; int main() { int x,y,z; for(int i=0;i<=9;i++) a[i]=i; do { if(!a[0] || !a[4]) continue; x=a[0]*1000+a[1]*100+a[2]*10+a[3]; y=a[4]*1000+a[5]*100+a[6]*10+a[1]; z=a[4]*10000+a[5]*1000+a[2]*100+a[1]*10+a[7]; if(x+y==z) printf("%d\n",y); }while(next_permutation(a,a+10)); return 0; }
相关文章推荐
- 全排列 next_permutation 简单用法
- 全排列next_permutation的用法(稍微有点改动)
- C++STL的next_permutation 的用法
- next_permutation(排列组合)函数的用法
- 关于全排列 next_permutation() 函数的用法
- 【leetcode】 Permutations 一个简单next_permutation的实现
- 全排列next_permutation的用法 HDU 1027 PKU 3187 1146 1731
- HDU1716 排列2 next_permutation的用法
- uva 101 例题5-2 vector以及string的简单用法
- next_permutation的简单应用HDU1207
- next_permutation的思想和用法
- [C++] next_permutation的函数 与 copy() 函数的用法
- next_permutation 与 prev_permutation的用法 (全排列)
- 关于next_permutation的用法
- 【STL】 next_permutation用法
- next_permutation用法
- [黑马程序员]_简单的try catch用法和几个小例题
- next_permutation(,)用法
- C++ STL 之 next_permutation 的用法(下一个序列函数,按字典序排)
- C++ STL, next_permutation用法。