Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define sf scanf
- #define pf printf
- vector<int> ara;
- vector<int> result;
- vector<int> res2[1000];
- bool mark[15],flag;
- int sum,total,n,INDEX;
- void call(int t)
- {
- if(sum==total)
- {
- res2[INDEX++]=result;
- flag=true;
- return;
- }
- else if(sum>total) return;
- for(int i=t;i<n;i++)
- {
- result.push_back(ara[i]);
- sum+=ara[i];
- call(i+1);
- result.pop_back();
- sum-=ara[i];
- }
- }
- bool cmp (vector<int>a,vector<int>b)
- {
- for(int i=0;i<a.size();i++)
- {
- if(a[i]==b[i]) continue;
- return a[i]>b[i];
- }
- return a.size()>b.size();
- }
- int main()
- {
- int i,x;
- while(sf("%d",&total))
- {
- sf("%d",&n);
- if(total==0&&n==0) break;
- //cout<<n<<endl;
- for(i=0;i<n;i++)
- {
- sf("%d",&x);
- ara.push_back(x);
- }
- //memset(mark,0,sizeof(mark));
- result.clear();
- flag=false;
- sum=0;
- INDEX=0;call(0);
- pf("Sums of %d:\n",total);
- if(!flag) pf("NONE\n");
- else
- {
- sort(res2,res2+INDEX,cmp);
- for(int j=0;j<res2[0].size();j++)
- {
- if(j==0)pf("%d",res2[0][j]);
- else
- {
- pf("+%d",res2[0][j]);
- }
- }
- pf("\n");
- for(i=1;i<INDEX;i++)
- {
- if(res2[i-1]==res2[i])
- continue;
- for(int j=0;j<res2[i].size();j++)
- {
- if(j==0)pf("%d",res2[i][j]);
- else
- {
- pf("+%d",res2[i][j]);
- }
- }
- pf("\n");
- }
- }
- for(i=0;i<INDEX;i++) res2[i].clear();
- ara.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment