Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.7 kB
2
Indexable
Never
#include <stdio.h>
#include <string.h>
const int max_lengh = 10;



int repeats(int *a, int size){
    int repeat[10] = {0};
    for (int i = 0; i < size - 1; i++){
        repeat[a[i]]++;
    }
    for (int i = 0; i < 10; i++){
        if (repeat[i] > 1){
            return 1;
        }
    }
    return 0;
}

void swap_elements(int *x, int *y) {
    int save = *x;
    *x = *y;
    *y = save;
}

int Permutation(int *a, int size){
    int i = size - 2;
    while (i >= 0 && a[i] > a[i + 1])
        i--;
    if (i == -1)
        return 0;
    for (int j = i + 1, end = size - 1; j < end; j++, end--)
        swap_elements(&a[j], &a[end]);
    int j = i + 1;
    while (a[j] < a[i])
        j++;
    swap_elements(&a[i], &a[j]);
    return 1;
}

int main(){
    char str[10] = "0123456789";
    int size = 0;
    int flag = 0;
    char curchar;
    int array[max_lengh];
    for (int i = 0; i < max_lengh + 1; i++){
//      array bad input
        scanf("%c", &curchar);
        if (strchr(str, curchar) == NULL || (curchar != '\n' && i == max_lengh)){
            flag = 1;
            break;
        }
        if (curchar == '\n'){
            break;
        }
        array[i] = curchar - '0';
        size++;
    }
//  count bad input
    int count;
    printf("%d", flag);
    scanf("%d", &count);
    if (count < 1 || repeats(array, size) || flag){
        printf("bad input");
        return 0;
    }
    int perm = 0;
    while (Permutation(array, size) && perm < count){
        perm++;
        for (int i = 0; i < size; i++)
            printf("%d", array[i]);
        printf("\n");
    }
    return 0;
}