Advertisement
Guest User

chestionar

a guest
Oct 17th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int st[100], n, s = 0;
  7. int punctaj[100];
  8. int a, b, p, q;
  9.  
  10. void citeste()
  11. {
  12.     ifstream f("chestionar.in");
  13.     f >> n;
  14.     for(int i = 1; i <= n; i++)
  15.         f >> punctaj[i];
  16.     f.close();
  17.  
  18.     cout << "a="; cin >> a;
  19.     cout << "b="; cin >> b;
  20.     cout << "p="; cin >> p;
  21.     cout << "q="; cin >> q;
  22. }
  23.  
  24. void init(int k)
  25. {
  26.     if(k == 1)
  27.         st[k] = 0;
  28.     else
  29.         st[k] = st[k-1];
  30. }
  31.  
  32. int succesor(int k)
  33. {
  34.     if(st[k] < n && k < n)
  35.     {
  36.         st[k]++;
  37.         return 1;
  38.     }
  39.     else
  40.     {
  41.         s -= punctaj[st[k-1]];
  42.         return 0;
  43.     }
  44. }
  45.  
  46. int valid(int k)
  47. {
  48. //    for(int i = 1; i<k; i++)
  49. //        if(st[i] == st[k])
  50. //            return 0;
  51.  
  52.     if(s + punctaj[st[k]] <= q)
  53.     {
  54.         s += punctaj[st[k]];
  55.         return 1;
  56.     }
  57.     return 0;
  58. }
  59.  
  60. int solutie(int k)
  61. {
  62.     return s >= p && k >= a && k <= b;
  63. }
  64.  
  65. void tipar(int k)
  66. {
  67.     cout << "Suma este: " << s << "  si k este: " << k << "      ";
  68.     for(int i = 1; i <= k; i++)
  69.         cout << punctaj[st[i]] << " ";
  70.     cout << endl;
  71.     s -= punctaj[st[k]];
  72. }
  73.  
  74.  
  75. void bkt(int k)
  76. {
  77.     init(k);
  78.     while(succesor(k))
  79.         if(valid(k))
  80.             if(solutie(k))
  81.                 tipar(k);
  82.             else
  83.                 bkt(k+1);
  84. }
  85.  
  86. int main()
  87. {
  88.     citeste();
  89.     for(int i = 0; i < b-a; i++)
  90.     {
  91.         bkt(1);
  92.         a ++;
  93.     }
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement