Recursive Implementation of full string Arrangement
# Include "stdafx. h" # include
# Include using namespace std; void permutation (char * pStr, char * pBegin) {assert (pStr & pBegin); if (* pBegin = '\ 0 ') cout <pStr <endl; else {for (char * pCh = pBegin; * pCh! = '\ 0'; ++ pCh) {swap (* pBegin, * pCh); permutation (pStr, pBegin + 1); swap (* pBegin, * pCh) ;}} int main () {char str [] = "abcd"; permutation (str, str );}
This operation is performed on the source string. Therefore,
SwapAnd then you need to try again
SwapBack to restore. The final result is grouped.
To
AStarts
BStarts
CStarts
DStart with, followed by three characters
First place a characterAAfter switching with the next character, you only need to recursively call the current function for the next three characters.. The recursion condition is that the string is empty.
* PBegin = '\ 0'.