#include "stdio.h"
#include "stdlib.h"
#define NULL 0
int *t;
void output();
void process();
void main(void)
{int *detmoney;
int i,k,money,account,sum,ini;
fprintf(stderr,"How much money in your packet?\nmoney=");
scanf("%d",&money);
printf("\n");
fprintf(stderr,"How many books do you want!\naccount=");
scanf("%d",&account);
printf("\n");
fprintf(stderr,"Please input details here:\n\t");
t=calloc(money,sizeof(int));
if (t==NULL)
{fprintf(stderr,"Out of Memories!\n");
exit(0);
}
detmoney=calloc(money,sizeof(int));
if (detmoney==NULL)
{fprintf(stderr,"Out of Memories!\n");
exit(1);
}
for (i=0;i<money;i++)
{fprintf(stderr,"detmoney[%d]=",i);
scanf("%d",&detmoney[i]);
printf("\n\t");
sum+=detmoney[i];
}
ini=0;
k=0;
process(detmoney,money,account,k,sum,ini);
free(t);
free(detmoney);
}
void process(detmoney,money,count,k,sum,ini)
int *detmoney;
int money,count,k,sum,ini;
{ int i=0;
if (ini+detmoney[i]==count)
{t[k]=detmoney[i];
output(k);
return;
}
else
if ((ini+sum>=count)&&(ini+detmoney[i]<count))
{ t[k]=detmoney[i];
process(&detmoney[i+1],money-1-i,count,k+1,sum-detmoney[i],ini+detmoney[i]);
}
if ((ini+sum>=count)&&(ini+detmoney[i+1]<=count))
process(&detmoney[i+1],money-i-1,count,k,sum-detmoney[i],ini);
}
void output(k)
int k;
{ int i;
for (i=0;i<=k;i++)
printf("%4d",t[i]);
printf("\n");
}