题目内容:
对字符串(数字,字母,符号)进行全排列,并统计全排列的种树
输入描述
输入一个字符串
输出描述
输出字符串的全排列,每种情况占一行,最后一行输出全排列的个数
输入样例
123
输出样例
123
132
213
231
312
321
6
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
string str;
int coun;
string s[100];int mySwap(int i, int j){char temp;temp = str[i];str[i] = str[j];str[j] = temp;
}int isswap(int i, int j){for(int k = i; k < j; k++){if(str[k] == str[j])return 0;}return 1;
} int allpai(int i, int n){int j;if(i == n - 1){
// cout << str << endl;s[coun++] = str;}else{for(int j = i; j < n; j++){if(isswap(i, j)){mySwap(i, j); //for循环每一次交换第一个与后面的每一个的位置 // cout << "test1 : i j " << i << " " << j << " str " << str << endl;allpai(i + 1, n); //从i = 0开始即确定了第一个位置,重复上面for循环动作,依次确定第二个位置 mySwap(j, i); //对到初始状态,为下个for循环做准备 // cout << "test1 : i j " << i << " " << j << " str " << str << endl;}}}
}bool cmp(string a, string b){return a < b;
}int main(){cin >> str;allpai(0, str.length());
// cout << "sort" << endl;sort(s, s + coun, cmp); for(int i = 0; i < coun; i++)cout << s[i] << endl;cout << coun;return 0;
}