UVA 357 Let Me Count The Ways

UVA_357

    用dp进行计数即可,状态转移方程为f[i,j]=Sum{f[i][j-k*a[i]]}(j-k*a[i]>=0,k为整数),其中i代表第i种面值,j代表当前有j的cents,a[i]为第i种面值的大小。

#include<stdio.h>
#include<string.h>
#define MAXD 30010
int d[] = {1, 5, 10, 25, 50};
long long int f[10][MAXD];
void prepare()
{
int i, j, k;
memset(f, 0, sizeof(f));
for(i = 0; i < MAXD; i ++)
f[0][i] = 1;
for(i = 1; i < 5; i ++)
for(j = 0; j < MAXD; j ++)
for(k = 0; k * d[i] <= j; k ++)
f[i][j] += f[i - 1][j - k * d[i]];
}
int main()
{
int k, n;
prepare();
while(scanf("%d", &n) == 1)
{
if(f[4][n] == 1)
printf("There is only 1 way to produce %d cents change.\n", n);
else
printf("There are %lld ways to produce %d cents change.\n", f[4][n], n);
}
return 0;
}


你可能感兴趣的:(count)