Advertisement
Iamtui1010

recursive_permutation.c

Mar 21st, 2023
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int n, cnt = 0;
  4. int b[100];
  5. int vst[100];
  6. FILE *fi, *fo;
  7.  
  8. void rc(int i)
  9. {
  10.     if (i > n){
  11.         ++cnt;
  12.         for (long j = 1; j <= n; ++j)
  13.             fprintf(fo, "%i", b[j]);
  14.         fprintf(fo, "%s", "\n");
  15.         return;
  16.     }
  17.     for (long j = 1; j <= n; ++j)
  18.         if (!vst[j]){
  19.             vst[j] = 1;
  20.             b[i]=j;
  21.             rc(i+1);
  22.             vst[j] = 0;
  23.         }
  24. }
  25.  
  26. int main()
  27. {
  28.     fi = fopen("in", "r");
  29.     fscanf(fi,"%d", &n);
  30.     fclose(fi);
  31.  
  32.     fo = fopen("out", "w");
  33.     rc(1);
  34.     fprintf(fo, "qtt: %i", cnt);
  35.     fclose(fo);
  36.  
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement