Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int x[20], n, nrsol = 0, nr[20], val[20], sum[20], S;
- int Valid(int k) {
- sum[k] = sum[k - 1] + val[k] * x[k];
- if (sum[k] > S)
- return 0;
- if (k == n && sum[k] != S) return 0;
- return 1;
- }
- int sol(int k) {
- return k == n;
- }
- void Afisare() {
- int i, j;
- for (i = 1; i <= n; i++)
- if (x[i] != 0) {
- cout << x[i] << "*" << val[i] << "lei";
- if (i != n && x[i + 1])
- cout << "+";
- }
- cout << endl;
- nrsol++;
- }
- void Back() {
- int k = 1, cand;
- x[1] = -1;
- while (k > 0) {
- cand = 0;
- while (cand == 0 && x[k] < nr[k]) {
- x[k]++;
- cand = Valid(k);
- }
- if (cand == 0)
- k--;
- else if (sol(k))
- Afisare();
- else {
- k = k + 1;
- x[k] = -1;
- }
- }
- }
- int main() {
- int i;
- cout << " Plata unei sume de bani" << endl;
- cout << "Suma: ";
- cin >> S;
- cout << endl << "Numarul tipuri monezi: ";
- cin >> n;
- cout << endl;
- for (i = 1; i <= n; i++) {
- cout << " Valoare moneda tip " << i << ": ";
- cin >> val[i];
- cout << " Numar monezi tip " << i << " : ";
- cin >> nr[i];
- }
- cout << endl << "Solutiile sunt: " << endl;
- Back();
- cout << endl << "Numar solutii: " << nrsol;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement