[背包九讲笔记] UVa 674 Coin Change

看了背包九讲,感觉写得很好,很清楚,很容易看懂(虽然有很多句子不通顺的地方)。

这道题虽然看上去上不是背包,但和完全背包的考虑方法有很相似。

完全背包的状态转移方程:   f[i, v] = max(f[i-1,v], f[i][v-c[i]]+w[i]);

使用一维数组,有:      f[v] = max(f[v], f[v-c[i]+w[i]), v = c[i]...Vmax;

这道题f[i,v]有类似的意义:使用前 i 种硬币恰好组成面值v的方法总数,

这样f[i,v] 包含了只使用前 i-1 种硬币组成面值v的方法种数,另外也包含了使用前 i 种硬币组成面值 v-c[i] 的方法种数,所以状态转移方程为:

              f[i, v] = f[i-1, v] + f[i, v-c[i]].

 1 # include <stdio.h>
2 # include <memory.h>
3
4 # define MAXC 7500
5
6 const int c[] = {0, 1, 5, 10, 25, 50};
7
8 long long int f[MAXC+1];
9
10 int main()
11 {
12 int i, v, vol;
13
14 memset(f, 0, sizeof(f));
15 for ( f[0] = i = 1; i <= 5; ++i)
16 for ( v = c[i]; v <= MAXC; ++v)
17 f[v] += f[v-c[i]];
18
19 while (~scanf("%d", &vol))
20 printf("%lld\n", f[vol]);
21
22 return 0;
23 }

第一次提交使用了const int maxc = 7500, 然后分配f[maxc+1],编译错误:

error: variably modified 'f' at file scope

还有个问题,360 老是提示这个exe文件含有木马。。不知是何缘故?

原文地址:https://www.cnblogs.com/JMDWQ/p/2431158.html