Advertisement
Guest User

Lungime bara repere [C++][Bkt]

a guest
Apr 7th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int L, n;
  6. int a[100]; //Vectorul cu repere
  7. int v[100]; //Vectorul solutiei
  8.  
  9. void afis(int k){
  10.     static int nr = 1;
  11.     cout << "\nSolutia #" << nr++ << " : ";
  12.     for(int i = 1; i <= k;i++) cout << v[i] << " ";
  13. }
  14.  
  15. void backt(int lung, int k){
  16.     for (int i=1; i <=n;i++){
  17.         v[k] = a[i];
  18.         lung += a[i];
  19.         if(lung == L) afis(k);
  20.         else if (lung < L) backt(lung,k+1);
  21.         lung -= a[i];
  22.         }
  23. }
  24.  
  25. int main()
  26. {
  27.     ifstream in("date.txt");
  28.     in >> L >> n;
  29.     for (int i=1;i<=n;i++) in >> a[i];
  30.  
  31.     backt(0,1);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement