Advertisement
Patey

Untitled

May 8th, 2021
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int st[100], vf,n;
  5.  
  6. void Init(int i)
  7. {
  8.     st[i] = 0;
  9. }
  10.  
  11. int Succesor(int k)
  12. {
  13.     if (st[k] < n)
  14.     {
  15.         st[k]++;
  16.         return 1;
  17.     }
  18.     return 0;
  19. }
  20.  
  21. int Solution(int k)
  22. {
  23.     int i,j,l;
  24.  
  25.     if (k < n)
  26.         return 0;
  27.  
  28.     for (i = 1; i < k; i++)
  29.     {
  30.         for (j = i + 1; j <= k; j++)
  31.         {
  32.             if (st[i] == st[j])
  33.                 return 0;
  34.         }
  35.     }
  36.  
  37.     for (i = 1; i < k-1;i++)
  38.     {
  39.         for (l = i + 1; l < k; l++)
  40.         {
  41.             for (j = l + 1; j <= k; j++)
  42.             {
  43.                 if (st[l] == ((st[i] + st[j])/2))
  44.                     return 0;
  45.             }
  46.         }
  47.     }
  48.     return 1;
  49. }
  50.  
  51. void Print()
  52. {
  53.     int i;
  54.     for (i = 1; i <= n; i++)
  55.     {
  56.         printf("%d ", st[i]);
  57.     }
  58.     printf("\n");
  59. }
  60.  
  61. void Back()
  62. {
  63.     int isS, isV=1;
  64.     vf = 1;
  65.     Init(vf);
  66.     while (vf > 0)
  67.     {
  68.         isS = 0;
  69.         if (vf <= n)
  70.         {
  71.             do {
  72.                 isS = Succesor(vf);
  73.             } while (isS && !isV);
  74.         }
  75.         if (isS)
  76.         {
  77.             if (Solution(vf))
  78.             {
  79.                 Print();
  80.             }
  81.             else
  82.             {    
  83.                 vf++;
  84.                 Init(vf);
  85.             }
  86.         }
  87.         else
  88.         {
  89.             vf--;
  90.         }
  91.     }
  92. }
  93.  
  94. void main()
  95. {
  96.     printf("Dati n= \n");
  97.     scanf("%d", &n);
  98.     printf("\n");
  99.     Back();
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement