uva357 - Let Me Count The Ways(动规,母函数)


母函数的应用,,,

两层循环,最外层循环是币值的循环这样就可以保证1+2 = 3和2+1 = 3的重复情况。

第二层循环的是钱数,每次用新的币值时就对原来的钱数更新一次,

代码如下:

#include <cstdio>
#include <cstring>
#define N 30005
int v[5] = {1,5,10,25,50};
long long c[N];
void dp()
{
    memset(c,0,sizeof(c));
    c[0] = 1;
    for(int i = 0; i < 5; i++)
    for(int j = v[i]; j < N; j++)
    {
        c[j] += c[j-v[i]];
    }
}
int main ()
{
    int total;
    dp();
    while(~scanf("%d",&total))
    {
        long long ans = c[total];
        if(ans==1)          printf("There is only 1 way to produce %d cents change.\n",total);
        else printf("There are %lld ways to produce %d cents change.\n",ans,total);
    }
    return 0;
}


 

你可能感兴趣的:(uva357 - Let Me Count The Ways(动规,母函数))