题意
给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = “1312”,
输出为:
1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211
解题思路
借助#include <algorithm>
库里的sort
和next_permutation
。
sort
函数先将字符串排序,然后通过next_permutation
函数对字符串进行排列。
参考代码
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
char s[10];
int main(){
while (cin>>s){
int len=strlen(s);
sort(s,s+len);
do{
cout<<s<<endl;
}while(next_permutation(s,s+len));
}
return 0;
}