题意:五种面额的钱,组成一个给定的钱有多少种方式。
题解:dp递归求解。。
#include <stdio.h>
#include <string.h>
const int N = 30005;
const int coin[5] = {1, 5, 10, 25, 50};
long long f[N][5];
int n;
long long dp(int s, int i) {
if (f[s][i] != -1)
return f[s][i];
f[s][i] = 0;
for (int j = i; j < 5 && coin[j] <= s; j++)
f[s][i] += dp(s - coin[j], j);
return f[s][i];
}
int main() {
memset(f, -1, sizeof(f));
for (int i = 0; i < 5; i++)
f[0][i] = 1;
while (scanf("%d", &n) != EOF) {
long long res = dp(n, 0);
if (res != 1)
printf("There are %lld ways to produce %d cents change.\n", res, n);
else
printf("There is only 1 way to produce %d cents change.\n", n);
}
return 0;
}