4 12 10 7 5 1
2
思路:就是个01背包问题类似,只是求的是最小值
代码:
# include <stdio.h> # include <string.h> # define min(a,b)a<b?a:b int main(void) { int i,j,m,n,a[52],dp[100001]; while (~scanf("%d%d", &n,&m) && m+n != 0) { memset(dp,‘a‘,sizeof(dp)); dp[0] =0; for (i = 1; i <= n; i++) scanf("%d", &a[i]); for (i = 1; i <= n; i++) for (j = a[i]; j <= m; j++) dp[j] = min(dp[j], dp[j-a[i]]+1); printf("%d\n", dp[m]); } return 0; }
原文地址:http://blog.csdn.net/java_oracle_c/article/details/41826789