Advertisement
aimon1337

Untitled

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