Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int st[100], n, s = 0;
- int punctaj[100];
- int a, b, p, q;
- void citeste()
- {
- ifstream f("chestionar.in");
- f >> n;
- for(int i = 1; i <= n; i++)
- f >> punctaj[i];
- f.close();
- cout << "a="; cin >> a;
- cout << "b="; cin >> b;
- cout << "p="; cin >> p;
- cout << "q="; cin >> q;
- }
- void init(int k)
- {
- if(k == 1)
- st[k] = 0;
- else
- st[k] = st[k-1];
- }
- int succesor(int k)
- {
- if(st[k] < n && k < n)
- {
- st[k]++;
- return 1;
- }
- else
- {
- s -= punctaj[st[k-1]];
- return 0;
- }
- }
- int valid(int k)
- {
- // for(int i = 1; i<k; i++)
- // if(st[i] == st[k])
- // return 0;
- if(s + punctaj[st[k]] <= q)
- {
- s += punctaj[st[k]];
- return 1;
- }
- return 0;
- }
- int solutie(int k)
- {
- return s >= p && k >= a && k <= b;
- }
- void tipar(int k)
- {
- cout << "Suma este: " << s << " si k este: " << k << " ";
- for(int i = 1; i <= k; i++)
- cout << punctaj[st[i]] << " ";
- cout << endl;
- s -= punctaj[st[k]];
- }
- void bkt(int k)
- {
- init(k);
- while(succesor(k))
- if(valid(k))
- if(solutie(k))
- tipar(k);
- else
- bkt(k+1);
- }
- int main()
- {
- citeste();
- for(int i = 0; i < b-a; i++)
- {
- bkt(1);
- a ++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement