Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- ofstream fout("iesire.txt");
- int st[100],i,k,n,s,a[100],b[100];
- void init()
- {
- st[k]=-1;
- }
- int succesor()
- {
- if(st[k]<b[k])
- {
- st[k]++;
- return 1;
- }
- else
- return 0;
- }
- int valid()
- {
- long p=0;
- for(i=1; i<=k; i++)
- p=p+(st[i]*a[i]);
- if(p<=s) return 1;
- else return 0;
- }
- int solutie()
- {
- long p=0;
- for(i=1; i<=k; i++)
- p=p+(st[i]*a[i]);
- if(p==s) return 1;
- else return 0;
- }
- void tipar ()
- {
- fout << endl;
- for (int i=1; i<= k-1; i++)
- if (st[i])
- fout << st[i] << "*"<<a[i]<< " + ";
- if (st[k])
- fout << st[k] << "*"<<a[k];
- }
- void back()
- {
- int AS;
- k=1;
- init();
- while (k>0)
- {
- do {}
- while ((AS=succesor())&& !valid());
- if(AS)
- {
- if(solutie())
- tipar();
- else if(k<n)
- {
- k++;
- init ();
- }
- }
- else
- {
- st[k]=-1;
- k--;
- }
- }
- }
- int main()
- {
- cin>>s;
- cin>>n;
- for(i=1; i<=n; i++)
- {
- cin>>a[i];
- b[i]=s/a[i];
- }
- back();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement