ACM 574 Sum It Up
作法:(1)14個for(2)DFS(我所提供的解法)
網友若想提供14個for的暴力解 可以貼上來
想法:1.請計算同樣數字的個數
2.請了解題目 才看看我的作法 不知道要怎麼說明
/*************************************************************/
#include<stdio.h>
#include<stdlib.h>
int num[14][2]={0},top=1,find=0;
int anstemp[14]={0};
int n,m;
void make(int sum,int now)
{
int a,b,c;
for(a=num[now][1];a>=0;a--)
{
if(sum+a*num[now][0]<n&&now+1<top)
{
anstemp[now]=a;
make(sum+a*num[now][0],now+1);
}
if(sum+a*num[now][0]==n)
{
anstemp[now]=a;
int ans[14]={0},anstop=0;
for(b=1;b<now+1;b++)
{
if(anstemp[b]!=0)
for(c=0;c<anstemp[b];c++,anstop++)
ans[anstop]=num[b][0];
}
for(b=0;b<anstop-1;b++)
printf("%d+",ans[b]);
printf("%d",ans[b]);
printf("\n");
find=1;
}
}
}
main()
{
while(scanf("%d %d",&n,&m)==2)
{
int a,b,c,temp;
if(n==0&&m==0) break;
top=1;find=0;num[0][0]=-1;
for(a=0;a<m;a++)
{
scanf("%d",&temp);
if(temp==num[top-1][0]) num[top-1][1]++;
else {num[top][0]=temp;num[top][1]=1;top++;}
}
printf("Sums of %d:\n",n);
make(0,1);
if(find==0) printf("NONE\n");
for(a=0;a<=13;a++) {num[a][0]=0;num[a][1]=0;}
}
return 0;
}