UVA_165
Because h + k <= 9, the data volume is not large, so you can directly enumerate and perform deep search.
There are two deep search functions, one of which is used to enumerate the values that can be continuously achieved. If you can spell out the current value through the existing face value, you can continue to maintain the status of the existing face value and continue the deep search, alternatively, you can create a new value equal to the current value and perform further search. when neither of the two methods can spell out the current value, the current value minus 1 is the maximum value that can be spelled out under the current face value. Another deep search function is used to determine whether the current value can be spelled out through the existing face value, you only need to select whether to select the stamp with the current face value in sequence and make the total number of stamps smaller than h. When the current face value can be spelled out at a certain time point, 1 is returned, if the current face value cannot be spelled out in all cases, 0 is returned.
#include<stdio.h>
#include<string.h>
int H,K,ans[15],res;
int judge(int target,int cur,int num,int tot,int *A,int end)
{
int i,j;
if(tot==target)
return 1;
else if(cur==end||num==H)
return 0;
if(judge(target,cur+1,num,tot,A,end))
return 1;
if(judge(target,cur,num+1,tot+A[cur],A,end))
return 1;
return 0;
}
void dfs(int cur,int *A,int end)
{
int i,j;
if(judge(cur,0,0,0,A,end))
dfs(cur+1,A,end);
if(end<K)
{
A[end]=cur;
end++;
dfs(cur+1,A,end);
end--;
A[end]=-1;
}
if(cur-1>res)
{
res=cur-1;
memcpy(ans,A,sizeof(ans));
}
}
int main()
{
int i,j,k,A[15];
while(1)
{
scanf("%d%d",&H,&K);
if(H==0)
break;
memset(A,-1,sizeof(A));
res=0;
dfs(1,A,0);
for(i=0;i<K;i++)
printf("%3d",ans[i]);
printf(" ->");
printf("%3d\n",res);
}
return 0;
}