Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void back(int k);
- void tipar(int k);
- bool valid(int k);
- bool cuplu(int a, int b);
- bool acelasi_sex(int a, int b);
- int n,nrpers,stiva[21];
- FILE *in,*out;
- int main()
- {
- in=fopen("back.in","rt");
- out=fopen("back.out","wt");
- fscanf(in, "%d",&n);
- nrpers=n*2;
- back(1);
- fclose(in);
- fclose(out);
- return 0;
- }
- void back(int k)
- {
- for(int val=1;val<=nrpers;val++)
- {
- stiva[k]=val;
- if(valid(k))
- if(k==nrpers)
- tipar(k);
- else
- back(k+1);
- }
- }
- void tipar(int k)
- {
- if(!cuplu(stiva[k], stiva[1] && !acelasi_sex(stiva[k], stiva[1])))
- {
- for(int i=1;i<=nrpers;i++)
- fprintf(out, "%d ",stiva[i]);
- fprintf(out, "\n");
- }
- }
- bool valid(int k)
- {
- if(k==1)
- return true;
- if(cuplu(stiva[k], stiva[k-1]))
- return false;
- if(acelasi_sex(stiva[k], stiva[k-1]))
- return false;
- for(int i=1;i<k;i++)
- if(stiva[k]==stiva[i])
- return false;
- return true;
- }
- bool cuplu(int a, int b)
- {
- if(a&1 && b==a+1)
- return true;
- if(b&1 && a==b+1)
- return true;
- return false;
- }
- bool acelasi_sex(int a, int b)
- {
- return !((a&1) ^ (b&1));
- }
Add Comment
Please, Sign In to add comment