Advertisement
a53

Plata2

a53
Oct 14th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #define MAX 7
  3. using namespace std;
  4. int n,S,b[MAX],x[MAX],y[MAX];
  5.  
  6. void citire()
  7. {
  8. cin>>n>>S;
  9. for(int i=1;i<=n;++i)
  10. cin>>b[i];
  11. for(int i=1;i<=n;++i)
  12. cin>>y[i];
  13. }
  14.  
  15. int suma(int k)
  16. {
  17. int s=0;
  18. for(int i=1;i<=k;++i)
  19. s+=x[i]*b[i];
  20. return s;
  21. }
  22.  
  23. int valid(int k)
  24. {
  25. return S>=suma(k);
  26. }
  27.  
  28. void scrie()
  29. {
  30. int nr=0;
  31. for(int i=1;i<=n;++i)
  32. if(x[i]>0)
  33. ++nr;
  34. if(nr==n)
  35. {
  36. for(int i=1;i<=n;++i)
  37. cout<<x[i]<<' ';
  38. cout<<'\n';
  39. exit(0);
  40. }
  41. }
  42.  
  43. void bk(int k)
  44. {
  45. if(k==n+1)
  46. {
  47. if(suma(n)==S)
  48. scrie();
  49. }
  50. else
  51. for(int i=0;i<=y[k];++i)
  52. {
  53. x[k]=i;
  54. if(valid(k))
  55. bk(k+1);
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. citire();
  62. bk(1);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement