标签:style color os io for amp size ios
题意:用所给的硬币面值构成所需的面值
思路:因为所用硬币数量不限,所以很容易想到完全背包。
递推:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 10005; int n; int coin[] = {1, 5, 10, 25, 50}; long long d[MAXN]; void dp() { memset(d, 0, sizeof(d)); d[0] = 1; for (int i = 0; i < 5; i++) for (int j = coin[i]; j < MAXN; j++) d[j] += d[j - coin[i]]; } int main() { dp(); while (scanf("%d", &n) != EOF) { printf("%lld\n", d[n]); } return 0; }
记忆化搜索:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 10005; int n; int coin[] = {1, 5, 10, 25, 50}; long long d[MAXN][6]; void init() { memset(d, -1, sizeof(d)); for (int i = 0; i < 5; i++) d[0][i] = 1; } long long dp(int s, int m) { if (d[s][m] != -1) return d[s][m]; d[s][m] = 0; for (int i = m; i < 5 && s >= coin[i]; i++) d[s][m] += dp(s - coin[i], i); return d[s][m]; } int main() { init(); while (scanf("%d", &n) != EOF) { long long ans = dp(n, 0); printf("%lld\n", ans); } return 0; }
UVA674- Coin Change,布布扣,bubuko.com
标签:style color os io for amp size ios
原文地址:http://blog.csdn.net/u011345461/article/details/38232513