Advertisement
rofllmaolol

plata unei sume gresit

Feb 26th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int st[500],i,k,n,s,a[500],b[500];
  4. void init()
  5. {
  6.     st[k]=-1;
  7. }
  8. int succesor()
  9. {
  10.  
  11.     if(st[k]<b[k])
  12.     {
  13.         st[k]++;
  14.         return 1;
  15.     }
  16.     else
  17.         return 0;
  18. }
  19. int valid()
  20. {
  21.     int p=0;
  22.         for(i=1;i<=k;i++)
  23.         p=p+(st[i]*a[i]);
  24.     if(p<=s) return 1;
  25.     else return 0;
  26.  
  27. }
  28. int solutie()
  29. {
  30.      int p=0;
  31.         for(i=1;i<=k;i++)
  32.         p=p+(st[i]*a[i]);
  33.     if(p==s) return 1;
  34.     else return 0;
  35. }
  36. void tipar()
  37. {
  38.     for(i=1;i<=k;i++)
  39.         cout<<st[i]<<"*"<<a[i]<<"+";
  40.     cout<<endl;
  41. }
  42. void back()
  43. {
  44.     int AS;
  45.     k=1;
  46.     init();
  47.     while (k>0)
  48.     {
  49.         do
  50.         {} while ((AS=succesor())&& !valid());
  51.     if(AS)
  52.         if(solutie())
  53.         tipar();
  54.     else
  55.     {
  56.         k++;
  57.         init ();
  58.     }
  59.     else {
  60.             st[k]=-1;
  61.     k--;
  62.  
  63.  
  64. }
  65. }}
  66. main()
  67. {
  68.     cin>>s;
  69.     cin>>n;
  70.     for(i=1;i<=n;i++)
  71.         cin>>a[i];
  72.     for(i=1;i<=n;i++)
  73.         b[i]=s/a[i];
  74.     back();
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement