Advertisement
STANAANDREY

plata suma

Nov 17th, 2020
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int x[20], n, nrsol = 0, nr[20], val[20], sum[20], S;
  4.  
  5. int Valid(int k) {
  6.     sum[k] = sum[k - 1] + val[k] * x[k];
  7.  
  8.     if (sum[k] > S)
  9.         return 0;
  10.     if (k == n && sum[k] != S) return 0;
  11.         return 1;
  12. }
  13.  
  14. int sol(int k) {
  15.     return k == n;
  16. }
  17.  
  18. void Afisare() {
  19.     int i, j;
  20.     for (i = 1; i <= n; i++)
  21.       if (x[i] != 0) {
  22.           cout << x[i] << "*" << val[i] << "lei";
  23.           if (i != n && x[i + 1])
  24.              cout << "+";
  25.       }
  26.     cout << endl;
  27.     nrsol++;
  28. }
  29.  
  30. void Back() {
  31.     int k = 1, cand;
  32.     x[1] = -1;
  33.     while (k > 0) {
  34.         cand = 0;
  35.         while (cand == 0 && x[k] < nr[k]) {
  36.             x[k]++;
  37.             cand = Valid(k);
  38.         }
  39.         if (cand == 0)
  40.             k--;
  41.         else if (sol(k))
  42.             Afisare();
  43.         else {
  44.             k = k + 1;
  45.             x[k] = -1;
  46.         }
  47.       }
  48. }
  49.  
  50. int main() {
  51.   int i;
  52.   cout << "                                            Plata unei sume de bani" << endl;
  53.   cout << "Suma: ";
  54.   cin >> S;
  55.   cout << endl << "Numarul tipuri monezi: ";
  56.   cin >> n;
  57.   cout << endl;
  58.   for (i = 1; i <= n; i++) {
  59.       cout << " Valoare moneda tip " << i << ": ";
  60.       cin >> val[i];
  61.       cout << " Numar monezi tip " << i << "  : ";
  62.       cin >> nr[i];
  63.   }
  64.   cout << endl << "Solutiile sunt: " << endl;
  65.   Back();
  66.   cout << endl << "Numar solutii: " << nrsol;
  67.   return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement