美文网首页
C语言递归实现全排列Perm

C语言递归实现全排列Perm

作者: 闫品品 | 来源:发表于2019-03-28 10:46 被阅读0次

    就是第一个数分别以后面的数进行交换

    假设输入E = (a , b , c),

    则 perm(E)= a.perm(b,c)+ b.perm(a,c)+ c.perm(a,b)

    然后a.perm(b,c)= ab.perm(c)+ ac.perm(b)= abc + acb.依次递归进行

    #include <stdio.h>

    #define SWAP(x, y, t) ((t) = (x), (x) = (y), (y) = (t))

    void perm(char *list, int i, int n);

    int main(int argc, const char * argv[]) {

        // insert code here...

        char list[] = "abcd";

        perm(list, 0, 3);

        return 0;

    }

    void perm(char *list, int i, int n){

        int j;

        if(i == n){

            for(j = 0; j <= n; j++){

                printf("%c", list[j]);

            }

            printf("    ");

        }

        else{

            for(j = i; j <= n; j++){

                swap(list[i], list[j]);

                perm(list, i + 1, n);

                swap(list[i], list[j]);

            }

        }

    }

    具体递归流程如下:

    ***perm(abcd), 0, 3***

             ***perm(abcd),1, 3***

                       ***perm(abcd),2, 3***

                                ***perm(abcd),3, 3***

                                abcd

                                ***perm(abdc),3, 3***

                                abdc

                       ***perm(acbd),2, 3***

                                ***perm(acbd),3, 3***

                                acbd

                                ***perm(acdb),3, 3***

                                acdb

                       ***perm(adcb),2, 3***

                                ***perm(adcb),3, 3***

                                adcb

                                ***perm(adbc),3, 3***

                                adbc

             ***perm(bacd),1, 3***

                       ***perm(bacd),2, 3***

                                ***perm(bacd),3, 3***

                                bacd

                                ***perm(badc),3, 3***

                                badc

                       ***perm(bcad),2, 3***

                                ***perm(bcad),3, 3***

                                bcad

                                ***perm(bcda),3, 3***

                                bcda

                       ***perm(bdca),2, 3***

                                ***perm(bdca),3, 3***

                                bdca

                                ***perm(bdac),3, 3***

                                bdac

             ***perm(cbad),1, 3***

                       ***perm(cbad),2, 3***

                                ***perm(cbad),3, 3***

                                cbad

                                ***perm(cbda),3, 3***

                                cbda

                       ***perm(cabd),2, 3***

                                ***perm(cabd),3, 3***

                                cabd

                                ***perm(cadb),3, 3***

                                cadb

                       ***perm(cdab),2, 3***

                                ***perm(cdab),3, 3***

                                cdab

                                ***perm(cdba),3, 3***

                                cdba

             ***perm(dbca),1, 3***

                       ***perm(dbca),2, 3***

                                ***perm(dbca),3, 3***

                                dbca

                                ***perm(dbac),3, 3***

                                dbac

                       ***perm(dcba),2, 3***

                                ***perm(dcba),3, 3***

                                dcba

                                ***perm(dcab),3, 3***

                                dcab

                       ***perm(dacb),2, 3***

                                ***perm(dacb),3, 3***

                                dacb

                                ***perm(dabc),3, 3***

                                dabc

    Program ended with exit code: 0

    相关文章

      网友评论

          本文标题:C语言递归实现全排列Perm

          本文链接:https://www.haomeiwen.com/subject/ijgfbqtx.html