Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <mm_malloc.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. void swap(int* a, int* b) {
  7.     int tmp = *a;
  8.     *a = *b;
  9.     *b = tmp;
  10. }
  11.  
  12. bool lastPerm(const int* arrOfPerm, int length)
  13. {
  14.     for (int i = 1; i < length; i++)
  15.     {
  16.         if (arrOfPerm[i] > arrOfPerm[i - 1])
  17.         {
  18.             return false;
  19.         }
  20.     }
  21.     return true;
  22. }
  23.  
  24. void permutations(int* arrOfPerm, int length, size_t countOfIterations)
  25. {
  26.     if (countOfIterations == 0 || lastPerm(arrOfPerm, length))
  27.     {
  28.         return;
  29.     }
  30.     int stop = 0, first = 0;
  31.     for(int i = length -1; i > 0; i--)
  32.     {
  33.         if (arrOfPerm[i] > arrOfPerm[i - 1])
  34.         {
  35.             stop = arrOfPerm[i - 1];
  36.             first = i - 1;
  37.             break;
  38.         }
  39.     }
  40.     int second = first + 1;
  41.     for (int i = first + 2; i < length; i++)
  42.     {
  43.         if (arrOfPerm[i] > stop && arrOfPerm[i] < arrOfPerm[second])
  44.         {
  45.             second = i;
  46.         }
  47.     }
  48.     swap(&arrOfPerm[first], &arrOfPerm[second]);
  49.     int l = first + 1;
  50.     int r = length - 1;
  51.     while (l < r)
  52.     {
  53.         swap(&arrOfPerm[l], &arrOfPerm[r]);
  54.         l++;
  55.         r--;
  56.     }
  57.  
  58.     for (int i = 0; i < length; i++)
  59.     {
  60.         printf("%d", arrOfPerm[i]);
  61.     }
  62.     printf("\n");
  63.     permutations(arrOfPerm, length, countOfIterations - 1);
  64. }
  65.  
  66.  
  67. int main() {
  68.     char* in = (char*)calloc(11, sizeof(char));
  69.     if(scanf("%10s", in) == 0)
  70.     {
  71.         printf("bad input");
  72.         free(in);
  73.         return 0;
  74.     }
  75.  
  76.     size_t countOfIterations;
  77.     if(scanf("%zu", &countOfIterations) == 0)
  78.     {
  79.         printf("bad input");
  80.         free(in);
  81.         return 0;
  82.     }
  83.  
  84.     int arrRepeat[10] = { 0 };
  85.     for(int i = 0; i < (int)strlen(in); i++)
  86.     {
  87.         if(in[i] < '0' || in[i] > '9' || arrRepeat[in[i] - '0'] == 1)
  88.         {
  89.             printf("bad input");
  90.             free(in);
  91.             return 0;
  92.         }
  93.         else
  94.         {
  95.             arrRepeat[in[i] - '0']++;
  96.         }
  97.     }
  98.  
  99.     int arrOfPerm[strlen(in)];
  100.     for(int i = 0; i < (int)strlen(in); i++)
  101.     {
  102.         arrOfPerm[i] = in[i] - '0';
  103.     }
  104.  
  105.     permutations(arrOfPerm, strlen(in), countOfIterations);
  106.     free(in);
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement