字符串的排列

来源:互联网 发布:360全景拍摄软件 编辑:程序博客网 时间:2024/05/22 03:30

题目描述

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
#include <iostream>using namespace std;void Permutation(char* pStr, char* pBegin){if(*pBegin == '\0')printf("%s\n",pStr);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);}

// ====================测试代码====================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[] = "abc";    Test(string4);    return 0;}

0 0
原创粉丝点击