///Permutation///#include#include void Swap(char* p1, char* p2) { char temp = *p1; *p1 = *p2; *p2 = temp;}void Permutation_bad(char* pStr, char* pStart);void Permutation(char* pStr) { if(pStr == NULL) return; Permutation_bad(pStr, pStr);}void Permutation_bad(char* pStr, char* pStart) { if(*pStart == '\0') { printf("%s.\n", pStr); return; } char* pIndex = pStart; while(*pIndex != '\0') { Swap(pStart, pIndex); Permutation_bad(pStr, pStart+1); Swap(pStart, pIndex); ++pIndex; }}// ====================测试代码====================void Test(char* pStr){ if(pStr == NULL) printf("Test for NULL begins:\n"); else printf("Test for %s begins:\n", pStr); Permutation(pStr); printf("\n");}int main(int argc, char* argv[]){ Test(NULL); char string1[] = ""; Test(string1); char string2[] = "a"; Test(string2); char string3[] = "ab"; Test(string3); char string4[] = "abcd"; Test(string4); return 0;}
Combination///#include#include #include #include void Combination_m(char* pStr, int m, std::vector & result);void Combination(char* pStr) { if(pStr == NULL || pStr == '\0') return; int number = strlen(pStr); for(int i=1; i<=number; ++i) { std::vector result; Combination_m(pStr, i, result); }}void Combination_m(char* pStr, int m, std::vector & result) { if(pStr == NULL || (*pStr == '\0' && m != 0)) return; if(m == 0) { for(std::vector ::iterator iter = result.begin(); iter