UVA 357 - Let Me Count The Ways

题意:给定一个总金额,用题中所给面额求有多少种分法
解法:先打表,一个数组储存面额,一个数组储存从1到30000所有面额的分法数目,从最小面额开始,每次循环,某一金额的分法都加上比它小面额的金额的分法
代码如下:

#include <bits/stdc++.h>

using namespace std;
int value[5]= {1,5,10,25,50};
const int M=30005;
long long int times[M];
int main()
{
    memset(times,0,sizeof(times));
    times[0]=1;
    for(int i=0; i<5; ++i)
        for(int j=value[i]; j<M; ++j)
            times[j]+=times[j-value[i]];
    int n;
    while(cin>>n)
    {
        if(times[n]-1)
            cout<<"There are "<<times[n]<<" ways to produce "<<n<<" cents change."<<endl;
        else
            cout<<"There is only 1 way to produce "<<n<<" cents change."<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liuxinyu666/article/details/80054719