Advertisement
aimon1337

Untitled

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