fork(2) download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int checkifsafe(char *s)
  5. {
  6. int sum =0;
  7. while(*s)
  8. {
  9. *s++=='{'?sum++:sum--;
  10. if(sum<-1)
  11. return 0;
  12. }
  13. return 1;
  14. }
  15.  
  16. void printset(char *s)
  17. {
  18. if(checkifsafe(s))
  19. {
  20. printf("{");
  21. while(*s)
  22. printf("%c",*s++);
  23. printf("}\n");
  24. }
  25. }
  26.  
  27. void initialize(char *s,int n)
  28. {
  29. int i;
  30. for(i=0;i<n;i++)
  31. {
  32. s[i] = '{';
  33. s[i + n] = '}';
  34. }
  35. s[2*n] = '\0';
  36. }
  37.  
  38. void swap(char *a,char *b)
  39. {
  40. char t = *a;
  41. *a = *b;
  42. *b = t;
  43. }
  44.  
  45. int check(char *s,int i,int c)
  46. {
  47. int j;
  48. for(j=c;j<i;j++)
  49. {
  50. if(s[j]==s[i])
  51. return 0;
  52. }
  53. return 1;
  54. }
  55.  
  56. void permute(char *s, int c, int n)
  57. {
  58. int i;
  59. if(c>=n-1)
  60. printset(s);
  61. else
  62. {
  63. for(i=c;i<n;i++)
  64. {
  65. if(check(s,i,c) || i==c)
  66. {
  67. swap(s+i,s+c);
  68. permute(s,c+1,n);
  69. swap(s+i,s+c);
  70. }
  71. }
  72. }
  73. }
  74.  
  75. int main(int argc,int **argv)
  76. {
  77. int n;
  78. printf("enter set number\n");
  79. scanf("%d",&n);
  80. n--;
  81. char *s = (char *)malloc(sizeof(2*n+1));
  82. initialize(s,n);
  83. permute(s,0,2*n);
  84. return 0;
  85. }
  86.  
stdin
5
compilation info
prog.c:75: warning: second argument of ‘main’ should be ‘char **’
prog.c: In function ‘main’:
prog.c:79: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
stdout
enter set number
{{{{{}}}}}
{{{{}{}}}}
{{{{}}{}}}
{{{{}}}{}}
{{{{}}}}{}
{{{}{{}}}}
{{{}{}{}}}
{{{}{}}{}}
{{{}{}}}{}
{{{}}{{}}}
{{{}}{}{}}
{{{}}{}}{}
{{{}}}{{}}
{{{}}}{}{}
{{}{{{}}}}
{{}{{}{}}}
{{}{{}}{}}
{{}{{}}}{}
{{}{}{{}}}
{{}{}{}{}}
{{}{}{}}{}
{{}{}}{{}}
{{}{}}{}{}
{{}}{{{}}}
{{}}{{}{}}
{{}}{{}}{}
{{}}{}{{}}
{{}}{}{}{}
{}{{{{}}}}
{}{{{}{}}}
{}{{{}}{}}
{}{{{}}}{}
{}{{}{{}}}
{}{{}{}{}}
{}{{}{}}{}
{}{{}}{{}}
{}{{}}{}{}
{}{}{{{}}}
{}{}{{}{}}
{}{}{{}}{}
{}{}{}{{}}
{}{}{}{}{}