题目:输入一个字符串,打印出该字符串中字符的所有排列。
思路:全排列,固定一个,排列剩余的,递归。
#include <iostream>
#include <string>
using namespace std;
void Permutation(char *pStr, char *pBegin) {
if (*pBegin == '\0')
cout << pStr << endl;
else {
for (char *pCh = pBegin; *pCh != '\0'; pCh++) {
char temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
Permutation(pStr, pBegin + 1);
temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
}
}
}
void Permutation(char *pStr) {
if (pStr == NULL)
return;
Permutation(pStr, pStr);
}
int main() {
char str[4] = "abc";
Permutation(str);
return 0;
}