Issue: Enter a string (requires no duplicate characters) to print out all permutations of characters in the character string.
Idea: Ask for all characters that may appear in the first position, swapping the first character with the character one by one after it. The first character is fixed, and all subsequent characters are arranged. This time throw all the following characters into two parts: the first character of the following character, and all the characters after the character, and then exchange the first character one after the other with the characters that follow it.
1#include <stdio.h>2#include"stdafx.h"3#include <tchar.h>4 5 voidPermutation (Char* PStr,Char*pbegin);6 7 voidPermutation (Char*pStr)8 {9 if(PStr = =NULL)Ten return; One A permutation (pStr, pStr); - } - the voidPermutation (Char* PStr,Char*pbegin) - { - if(*pbegin = =' /') - { +printf"%s\n", pStr); - } + Else A { at for(Char* PCh = Pbegin; *pch! =' /'; ++pCh) - { - Chartemp = *pCh; -*pch = *Pbegin; -*pbegin =temp; - inPermutation (pStr, Pbegin +1); - totemp = *pCh; +*pch = *Pbegin; -*pbegin =temp; the } * } $ }Panax Notoginseng - intMain () the { + A Char string[] ="ABC"; thePermutation (string); + - return 0; $}
Arrangement of characters