hdu 1258 Sum It Up
#include<algorithm>
#include<string.h>
using namespace std;
int flag,t,n,a[20],use[20],ans[20];
bool cmp(int x,int y)
{
return x>y;
}
void dfs(int sum,int now,int cnt)
{
int i;
if(sum==t)
{
flag++;
printf("%d",ans[0]);
for(i=1; i<cnt; i++)
{
printf("+%d",ans[i]);
}
printf("\n");
return ;
}
for(i=now; i<n; i++)
{
if(use[i]==0)
{
if(i!=0&&a[i-1]==a[i]&&use[i-1]==0) continue;
if(sum+a[i]>t) continue;
ans[cnt]=a[i];
use[i]=1;
dfs(sum+a[i],i+1,cnt+1);
use[i]=0;
}
}
}
int main()
{
int i;
while(~scanf("%d%d",&t,&n))
{
if(t==0&&n==0) break;
for(i=0; i<n; i++)
scanf("%d",&a[i]);
memset(use,0,sizeof(use));
sort(a,a+n,cmp);
flag=0;
printf("Sums of %d:\n",t);
dfs(0,0,0);
if(!flag) printf("NONE\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/