How to find permutation of k in a given length?

前端 未结 6 676
小鲜肉
小鲜肉 2020-12-05 17:01

How can I find the permutations of k in a given length?

For example:

The word cat has 3 letters: How can I find all the permutations of 2 in the

6条回答
  •  执笔经年
    2020-12-05 17:12

    void Prem (char *str, int k, int length) {
        if (k == length-1){
             printf("%s\n",str);
             return;
        } else {
            for (int i = k ; i < length; ++i) {
                char t = str[k];
                str[k] = str[i];
                str[i] = t;
                Prem(str,k+1,length);
                t = str[k];
                str[k] = str[i];
                str[i] = t;
            }
        }
    }
    

提交回复
热议问题