37-字符的全排列
題目?jī)?nèi)容:
對(duì)字符串(數(shù)字,字母,符號(hào))進(jìn)行全排列,并統(tǒng)計(jì)全排列的種樹(shù)
輸入描述
輸入一個(gè)字符串
輸出描述
輸出字符串的全排列,每種情況占一行,最后一行輸出全排列的個(gè)數(shù)
輸入樣例
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循環(huán)每一次交換第一個(gè)與后面的每一個(gè)的位置 // cout << "test1 : i j " << i << " " << j << " str " << str << endl;allpai(i + 1, n); //從i = 0開(kāi)始即確定了第一個(gè)位置,重復(fù)上面for循環(huán)動(dòng)作,依次確定第二個(gè)位置 mySwap(j, i); //對(duì)到初始狀態(tài),為下個(gè)for循環(huán)做準(zhǔn)備 // 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; }?
轉(zhuǎn)載于:https://www.cnblogs.com/zhumengdexiaobai/p/7735058.html
總結(jié)
- 上一篇: 远程服务器端口是否开放(审计)
- 下一篇: 莫名的证书错误...ERROR ITMS