1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <stdio.h>
#include <stdlib.h>
int main()
{
int temp,i,j,n,m,a[10000],b[10000],c[10000];
printf("Put the number: ");
scanf("%d",&n);
temp=n;
printf("\nHow much peaces?: ");
scanf("%d",&m);
printf("\n");
for(i=1;i<=m;i++)
{
printf("Put the value of peace[No.%d]: ",i);
scanf("%d",&a[i]);
}
for(i=1;i<=m;i++)
{
for(j=n;j>=1;j--)
{
if(a[i]<=j)
{
if(b[j]<a[i]+b[j-a[i]])
{
b[j]=a[i]+b[j-a[i]];
c[j]=i;
}
}
}
}
i=b[n];
while(n>0)
{
if(temp==n)
{
printf("\n*** The best combination is: ");
printf("(");
while(i>0)
{
printf("%d,",a[c[i]]);
i-=a[c[i]];
}
printf(") -> %d less than %d ***\n\nAnother combinations are:\n",temp-b[n],b[n]);
}
else
{
printf("(");
while(i>0)
{
printf("%d,",a[c[i]]);
i-=a[c[i]];
}
printf(") -> %d less than %d\n",temp-b[n],b[n]);
}
n--;
i=b[n];
}
system("pause");
return 0;
}
|