给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = "1312",
输出为:
1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211
Input
输入一个字符串S(S的长度 <= 9,且只包括0 - 9的阿拉伯数字)
Output
输出S所包含的字符组成的所有排列
Sample Input
1312
Sample Output
1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211
题意即为:已经很明确了,全排列!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[10], p[10], n;
void dfs(int var)
{
int i;
if(var==n)
{
for(int i=0; i<n; i++)
cout<<a[i];
cout<<endl;
return ;
}
for (int i=0; i<10; i++)
{
if (p[i]!=0)
{
a[var]=i;
p[i]--;
dfs(var+1);
p[i]++;
}
}
}
int main()
{
char q[10];
cin>>q;
n=strlen(q);
for(int i=0; q[i]!=0; i++)
p[q[i]-'0']++;
dfs(0);
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 15;
char s[MAX];
int main()
{
scanf("%s", s);
int lenth = strlen(s);
std::sort(s, s + lenth);
printf("%s\n", s);
while (std::next_permutation(s, s + lenth))
{
printf("%s\n",s);
}
return 0;
}
补充:
①关于next_permutation