http://acm.hdu.edu.cn/showproblem.php?pid=1258 #include<iostream> using namespace std; #define N 15 int a[N],ans[N]; int num[101],real[N]; bool flag; int m,sum; void dfs(int x,int index,int s) { if(s==sum) { flag=1; printf("%d",ans[0]); for(int i=1;i<index;i++) printf("+%d",ans[i]); printf("/n"); return; } if(s>sum||x>=m) return; for(int i=num[real[x]];i>=0;i--) { int temp=s+i*real[x]; if(temp>sum) continue; int j; for(j=index;j<=i+index;j++) ans[j]=real[x]; dfs(x+1,i+index,temp); } } int main(void) { int n; while(scanf("%d%d",&sum,&n),n) { m=0; memset(num,0,sizeof(num)); for(int i=0;i<n;i++) { scanf("%d",&a[i]); if(num[a[i]]==0) real[m++]=a[i]; num[a[i]]++; } flag=0; printf("Sums of %d:/n",sum); dfs(0,0,0); if(flag==0) printf("NONE/n"); } } dfs(int x,int index,int s)中,x代表搜索到那种数了,index存放好的结果的位数,s代表当前的和