Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 重复打印排序排列_C_Permutation - Fatal编程技术网

C 重复打印排序排列

C 重复打印排序排列,c,permutation,C,Permutation,我想以字典顺序重复打印字符串的所有排列。我写这段代码: char *input; void swap(char *x, char *y); void permute(char *str); int factorial(int n); void swapSort(char array[], int left, int right); void quick_sort(char *array, int left, int right); //void permute(char *str,

我想以字典顺序重复打印字符串的所有排列。我写这段代码:

char *input;

void swap(char *x, char *y);

void permute(char *str);

int factorial(int n);

void swapSort(char array[], int left, int right);

void quick_sort(char *array, int left, int right);

//void permute(char *str, char *p_ch, int length);

int main() {
    input = malloc(8 + 1 * sizeof(char));
    fgets(input, 9, stdin);
    int n = strlen(input);
    if (input[n - 1] == '\n') {
        n--;
        input[n] = '\0';
    }
    printf("Length of string: %d\n", n);
    printf("Input string: \"%s\"\n", input);
    quick_sort(input, 0, n);
    printf("sorted string: \"%s\"\n", input);
    printf("Number of permutations: %d\n", factorial(n));
    permute(input);
    //free(input);
    return 0;
}

int factorial(int n) {
    if (n == 1) return 1;
    return n * factorial(n - 1);

}


int compare(const void *a, const void *b) {
    return (*(char *) a - *(char *) b);
}


void permute(char *str) {
    int strSize = strlen(str);
    qsort(str, strSize, sizeof(char), compare);

    int endIsNotReached = true;
    int tmpSize;
    while (endIsNotReached) {

        printf("\"%s\"\n", str);
        for (tmpSize = strSize - 2; tmpSize > -1 && str[tmpSize] >= str[tmpSize + 1]; tmpSize--) {
            //do nothing
        }

        if (tmpSize > -1) {
            int j = 1 + tmpSize;
            for (int index = j; index < strSize && str[index]; index++) {
                if (str[index] < str[j] && str[index] > str[tmpSize])
                    j = index;
            }

            swap(&str[tmpSize], &str[j]);

            qsort(str + tmpSize + 1, strSize - 1 - tmpSize, sizeof(char), compare);
        }
        else {
            endIsNotReached = false;
        }
    };
}
void quick_sort(char *array, int left, int right) {
    if (left < right) {
        int boundary = left;
        for (int i = left + 1; i < right; i++) {
            if (array[i] < array[left]) {
                swapSort(array, i, ++boundary);
            }
        }
        swapSort(array, left, boundary);
        quick_sort(array, left, boundary);
        quick_sort(array, boundary + 1, right);
    }

}
void swapSort(char array[], int left, int right) {
    char tmp = array[right];
    array[right] = array[left];

    array[left] = tmp;
}


void swap(char *left, char *right) {
    char temp = *left;
    *left = *right;
    *right = temp;
}
char*输入;
无效交换(字符*x,字符*y);
无效排列(字符*str);
整数阶乘(intn);
void swapSort(字符数组[],左整数,右整数);
void quick_sort(字符*数组,左整数,右整数);
//void排列(char*str,char*p_ch,int-length);
int main(){
输入=malloc(8+1*sizeof(char));
fgets(输入,9,标准输入);
int n=strlen(输入);
如果(输入[n-1]='\n'){
n--;
输入[n]='\0';
}
printf(“字符串长度:%d\n”,n);
printf(“输入字符串:\%s\”\n,输入);
快速排序(输入,0,n);
printf(“排序字符串:\%s\”\n,输入);
printf(“置换数:%d\n”,阶乘(n));
排列(输入);
//免费(输入);
返回0;
}
整数阶乘(整数n){
如果(n==1)返回1;
返回n*阶乘(n-1);
}
整数比较(常数无效*a,常数无效*b){
返回(*(字符*)a-*(字符*)b);
}
无效排列(字符*str){
int strSize=strlen(str);
qsort(str、strSize、sizeof(char)、compare);
int endisnotreach=true;
int tmpSize;
while(endisnotreach){
printf(“\%s\”\n”,str);
对于(tmpSize=strSize-2;tmpSize>-1&&str[tmpSize]>=str[tmpSize+1];tmpSize--){
//无所事事
}
如果(tmpSize>-1){
int j=1+tmpSize;
对于(int index=j;indexstr[tmpSize])
j=指数;
}
交换(&str[tmpSize]、&str[j]);
qsort(str+tmpSize+1,strSize-1-tmpSize,sizeof(char),compare);
}
否则{
endisnotreach=false;
}
};
}
无效快速排序(字符*数组,左整数,右整数){
if(左<右){
int边界=左;
for(int i=left+1;i
但当我想打印字符串“aaa”时,输出仅为“aaa”,但我想输出其中的“aaa”三次。
(另一个例子是<强>输入-AAB”->强>输出“AAB”、“AAB'、“ABA”、“ABA”、“BAA”、“BAA”

P >即使OP不能使用C++和它的标准库,我们也可以在C中查看并实现它,以利用其字典序结果。

bool next_permutation( char *first, char *last ) {
    if (first == last)
        return false;
    char *i = last;
    if (first == --i)
        return false;

    char *i1,
         *i2;

    while (true) {
        i1 = i;
        if ( *--i < *i1 ) {
            i2 = last - 1;
            while ( *i >= *i2 ) --i2;
            swap(i, i2);
            reverse(i1, last);
            return true;
        }
        if (i == first) {
            reverse(first, last);
            return false;
        }
    }   
}

void reverse( char *first, char *last ) {
    while ((first != last) && (first != --last)) {
        swap(first, last);
        ++first;
    }
}
我还要稍微修改
main()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> 
// this is  ^^^  for bool, but if if C99 is not an option:
//    typedef enum { false = 0, true = !false } bool;


int factorial(int n);
void swap( char *x, char *y);
void reverse( char *first, char *last );   // used by next_permutation
bool next_permutation( char *first, char *last );
void permute( char *str, int length );

enum { WORD_MAX_SIZE = 16, ARRAY_SIZE };

int main(void) {
    char input[ARRAY_SIZE];

    fgets(input, ARRAY_SIZE, stdin);
    int n = strlen(input);
    if ( input[n-1] == '\n' ) {
        --n;
        input[n] = '\0';       
    }

    printf("Length of string: %d\n", n);
    printf("Input string: \"%s\"\n", input);

    printf("Number of permutations: %d\n", factorial(n));
    permute(input, n);

    return 0;
}

int factorial( int x ) {
    int f = x;
    while ( x > 1 ) {
        f *= --x;
    }
    return f;
}

int compare(const void *a, const void *b) {
    return (*(char *) a - *(char *) b);
}

void swap(char *left, char *right) {
    char temp = *left;
    *left = *right;
    *right = temp;
}

@MohitJain会是什么样子?我从未见过这样的事pair@MohitJain因为我只能用C语言工作,不能用C++语言。很抱歉我的无知。你仍然可以对每个字符使用一些标记来区分相似的字符。是的,一些标记,但我不知道确切的方法。如果我知道,我不会问:)你想要调试还是解决方案?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> 
// this is  ^^^  for bool, but if if C99 is not an option:
//    typedef enum { false = 0, true = !false } bool;


int factorial(int n);
void swap( char *x, char *y);
void reverse( char *first, char *last );   // used by next_permutation
bool next_permutation( char *first, char *last );
void permute( char *str, int length );

enum { WORD_MAX_SIZE = 16, ARRAY_SIZE };

int main(void) {
    char input[ARRAY_SIZE];

    fgets(input, ARRAY_SIZE, stdin);
    int n = strlen(input);
    if ( input[n-1] == '\n' ) {
        --n;
        input[n] = '\0';       
    }

    printf("Length of string: %d\n", n);
    printf("Input string: \"%s\"\n", input);

    printf("Number of permutations: %d\n", factorial(n));
    permute(input, n);

    return 0;
}

int factorial( int x ) {
    int f = x;
    while ( x > 1 ) {
        f *= --x;
    }
    return f;
}

int compare(const void *a, const void *b) {
    return (*(char *) a - *(char *) b);
}

void swap(char *left, char *right) {
    char temp = *left;
    *left = *right;
    *right = temp;
}
Input string: "aba"
Number of permutations: 6
    1: aab
    2: aab
    3: aba
    4: aba
    5: baa
    6: baa

Input string: "baca"
Number of permutations: 24
    1: aabc
    2: aabc
    3: aacb
    4: aacb
    5: abac
    6: abac
    7: abca
    8: abca
    9: acab
   10: acab
   11: acba
   12: acba
   13: baac
   14: baac
   15: baca
   16: baca
   17: bcaa
   18: bcaa
   19: caab
   20: caab
   21: caba
   22: caba
   23: cbaa
   24: cbaa