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