Alx09

Untitled

May 7th, 2020
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int v[20], a[20], i, n, k, m = 0, p;
  5.  
  6. void Init(int k) { // k este vârful stivei
  7.     v[k] = 0;
  8.     //initializeaz valoarea din vârful stivei
  9. }
  10.  
  11. int Succesor(int k) {
  12.     if (v[k] < n) {  // se poate creste valoarea din vârf
  13.         v[k]++;  // se incrementeaz valoarea din vârf
  14.         return 1; // functia a avut succes
  15.     }
  16.     else
  17.         // nu se poate creste valoarea din vârf
  18.         return 0;
  19. }
  20.  
  21. int rezolvare(int k) {
  22.     int r;
  23.     if (k < 2)
  24.         return 0;
  25.     r = a[v[k]] - a[v[k - 1]];
  26.     if (r <= 0)
  27.         return 0;
  28.     for (i = 1; i < k; i++)
  29.         if ((a[v[i + 1]] - a[v[i]]) != r)
  30.             return 0;
  31.  
  32.     return 1;
  33. }
  34.  
  35. int Valid(int k) {
  36.     int i;
  37.     for (i = 1; i < k; i++)
  38.         if (v[i] == v[k] ) return 0;
  39.     for (i = 1; i < v[k]; i++)
  40.         if (a[i] == a[v[k]]) return 0;
  41.    
  42.     return 1;
  43. }
  44.  
  45. int Solution(int k) {
  46.     return (k == n);
  47. }
  48.  
  49. void Print(int k) {
  50.     printf("%d : ", ++m);
  51.     for (i = 1; i <= k; i++)
  52.         printf("%d ", a[v[i]]);
  53.  
  54.     printf("\n");
  55.  
  56. }
  57.  
  58. void Back(int k) {
  59.     // B. recursiv
  60.     Init(k);
  61.     while (Succesor(k)) {
  62.         if (Valid(k)) {
  63.             if (rezolvare(k))
  64.                 Print(k);
  65.             if (Solution(k));
  66.                 else Back(k + 1);
  67.            
  68.         }
  69.     }
  70. }
  71.  
  72. int main(void) {
  73.     //printf("Dati n: ");
  74.     FILE *f;
  75.     f = fopen("in.txt", "r");
  76.     fscanf(f, "%d", &n);
  77.     for (i = 1; i <= n; i++)
  78.         fscanf(f, "%d", &a[i]);
  79.  
  80.     Back(1);
  81.  
  82.     system("pause");
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment