Advertisement
AlexandruDu

Problema 1

Oct 20th, 2020
2,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int st[20], n;
  5. ofstream fout("cifre.out");
  6.  
  7. void tipar(int k) {
  8.     for (int i = 1; i <= k; i++)
  9.         fout << st[i] - 1;
  10.     fout << endl;
  11. }
  12.  
  13. int valid(int k) {
  14.     if (k > 2 && st[k] % 2 == st[k - 1] % 2 && st[k] % 2 == st[k - 2] % 2)
  15.         return 0;
  16.     return 1;
  17. }
  18.  
  19. int sol(int k) {
  20.     return k == n;
  21. }
  22.  
  23. void bktr() {
  24.     int k = 1;
  25.     st[k] = 1;
  26.     while (k) {
  27.         if (st[k] < 10) {
  28.             st[k]++;
  29.             if (valid(k)) {
  30.                 if (sol(k))
  31.                     tipar(k);
  32.                 else {
  33.                     k++;
  34.                     st[k] = 0;
  35.                 }
  36.             }
  37.         }
  38.         else
  39.             k--;
  40.     }
  41. }
  42.  
  43. int main() {
  44.     cin >> n;
  45.     bktr();
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement