Advertisement
Alx09

Untitled

Jul 7th, 2020
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define natural unsigned short // csf sa mai pierd timp
  5.  
  6. int v[7]; //  the stack
  7. void Init(natural k) { // initialzation function
  8.     v[k] =  -1;
  9. }
  10. natural Succesor(natural k) {
  11.     if (v[k] < 9) { // will generate the number until 9
  12.         v[k]++;
  13.         return 1;
  14.     }
  15.     return 0;
  16. }
  17.  
  18. natural Valid(natural k) {
  19.     natural i;
  20.     if (v[k] % 2 == 1) return 1; // if is odd is valid
  21.     for (i = 1; i < k; i++) // else we search if is a copy of number in vector
  22.         if (v[i] == v[k])
  23.             return 0; //if we fiind a copy is a invalid combination
  24.     return 1;
  25. }
  26.  
  27. natural valid2() { // we search if are exactly 3 numbers in vector
  28.     natural nr = 0, i;
  29.     for (i = 1; i <= 6; i++) {
  30.         if (v[i] % 2 == 0) nr++;
  31.     }
  32.  
  33.     if (nr != 3) return 0; // if are not 3 numbers in vector is an invlid case
  34.     return 1;
  35. }
  36.  
  37. natural Solution(natural k) {
  38.     return (k == 6) && valid2(); // if we have 6 elements and 3 of them are disticnt even digits is a valid case
  39. }
  40.  
  41. void Print() {
  42.     natural i;
  43.     printf("0721 "); // fixed digits
  44.     for (i = 1; i <= 6; i++) // digits generated
  45.         printf("%d", v[i]);
  46.     printf(";\n");
  47. }
  48.  
  49. void Back() { // generaly form of bactrack function
  50.     natural k = 1, isS, isV;
  51.     Init(k);
  52.     while (k > 0) {
  53.         isS = 0; isV = 0;
  54.         if (k <= 6)
  55.             do {
  56.                 isS = Succesor(k);
  57.                 if (isS) isV = Valid(k);
  58.             } while (isS && !isV);
  59.             if (isS)
  60.                 if (Solution(k))
  61.                     Print();
  62.                 else {
  63.                     k++;
  64.                     Init(k);
  65.                 }
  66.             else
  67.                 k--;
  68.     }
  69. }
  70.  
  71. int main() {
  72.        
  73.     Back(); // main call
  74.     system("pause");
  75.     return 0;
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement