Guest User

Untitled

a guest
Jun 19th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. void back(int k);
  3. void tipar(int k);
  4. bool valid(int k);
  5. bool cuplu(int a, int b);
  6. bool acelasi_sex(int a, int b);
  7. int n,nrpers,stiva[21];
  8. FILE *in,*out;
  9. int main()
  10. {
  11.     in=fopen("back.in","rt");
  12.     out=fopen("back.out","wt");
  13.     fscanf(in, "%d",&n);
  14.     nrpers=n*2;
  15.     back(1);
  16.    
  17.     fclose(in);
  18.     fclose(out);
  19.     return 0;
  20. }
  21.  
  22.  
  23. void back(int k)
  24. {
  25.     for(int val=1;val<=nrpers;val++)
  26.     {
  27.         stiva[k]=val;
  28.         if(valid(k))
  29.             if(k==nrpers)
  30.                 tipar(k);
  31.             else
  32.                 back(k+1);
  33.     }
  34. }
  35.  
  36. void tipar(int k)
  37. {
  38.     if(!cuplu(stiva[k], stiva[1] && !acelasi_sex(stiva[k], stiva[1])))
  39.     {
  40.         for(int i=1;i<=nrpers;i++)
  41.             fprintf(out, "%d ",stiva[i]);
  42.         fprintf(out, "\n");
  43.     }
  44. }
  45.  
  46. bool valid(int k)
  47. {
  48.     if(k==1)
  49.         return true;
  50.     if(cuplu(stiva[k], stiva[k-1]))
  51.         return false;
  52.     if(acelasi_sex(stiva[k], stiva[k-1]))
  53.         return false;
  54.  
  55.     for(int i=1;i<k;i++)
  56.         if(stiva[k]==stiva[i])
  57.             return false;
  58.  
  59.     return true;
  60. }
  61.  
  62. bool cuplu(int a, int b)
  63. {
  64.     if(a&1 && b==a+1)
  65.         return true;
  66.  
  67.     if(b&1 && a==b+1)
  68.         return true;
  69.  
  70.     return false;
  71. }
  72.  
  73.  
  74. bool acelasi_sex(int a, int b)
  75. {
  76.     return !((a&1) ^ (b&1));
  77. }
Add Comment
Please, Sign In to add comment