算法题目---字符串的排列

来源:互联网 发布:淘宝图防盗图怎么设置 编辑:程序博客网 时间:2024/06/04 18:38

输入一个字符串,打印出该字符串中字符的所有排列。


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()
{
    Test(NULL);

    char string1[] = "";
    Test(string1);

    char string2[] = "a";
    Test(string2);

    char string3[] = "ab";
    Test(string3);

    char string4[] = "abc";
    Test(string4);

    return 0;

}


还未能理解程序逻辑

原创粉丝点击