Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- using namespace std;
- int N, V[1000];
- long M[1000], Max[1000], S;
- ofstream G("iesire.txt");
- void Citeste ()
- {
- cin>> N>> S;
- for (int i=1; i<= N; i++)
- {
- cin>>M[i];
- Max[i] = S / M[i];
- }
- // F.close();
- }
- int Solutie (int k)
- {
- long S=0;
- for (int i=1; i<= k; i++)
- S += V[i] * M[i];
- return ( ::S == S );
- }
- void Tipar (int k)
- {
- G << endl;
- for (int i=1; i<= k-1; i++)
- if (V[i])
- G << V[i] << "*"<<M[i]<< " + ";
- if (V[k])
- G << V[k] << "*"<<M[k];
- }
- int Valid (int k)
- {
- long S=0;
- for (int i=1; i<= k; i++)
- S += V[i] * M[i];
- return ( ::S >= S );
- }
- void Monede_Back ()
- {
- int k;
- for (k=1; k<=N; k++)
- V[k]=-1;
- k=1;
- while (k>0)
- {
- while (V[k]<Max[k])
- {
- V[k] ++;
- if (Valid(k))
- {
- if (Solutie(k))
- Tipar (k);
- else if (k<N) k++;
- }
- else
- V[k]=Max[k];
- }
- V[k--]=-1;
- }
- }
- int main ()
- {
- Citeste();
- Monede_Back ();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement