1.8 [hduoj] 2069 Coin Change

Problem Description

Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.

Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.

Input

The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.

Output

For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

Sample Input

11

26

Sample Output

4

13

#include<stdio.h>
int v[5]={1,5,10,25,50};
int dp[7500][101];        //行数表示面值,列数表示硬币值
void main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n<100)
        {
            for(int i=0;i<=n;i++)
            {
                for(int j=0;j<=n;j++)
                    dp[i][j]=0;
            }
        }
        else
        {
            for(int i=0;i<=n;i++)
            {
                for(int j=0;j<=100;j++)
                    dp[i][j]=0;
            }
        }
        //初始化
        dp[0][0]=1;
        for(int i=0;i<5;i++)
        {
            for(int va=v[i];va<=7500;va++)
            {
                for(int j=1;j<=100;j++)
                {
                    dp[va][j]+=dp[va-v[i]][j-1];
                }
            }
        }
        int sum=0;
        for(int i=0;i<=100;i++)
            sum+=dp[n][i];
        printf("%d\n",sum);

    }
}

这题似乎也是动态规划,需要用一个二维数组。

dp[i][j]表示的是i元用j个硬币有几种方法。

用v[]={1,5,10,25,50}来表示各个硬币的价值。va来表示各个硬币价值。

把dp[i][j]全部设为0,dp[0][0]=1;

关系式即为dp[i][j]+=dp[i-va][j-1](i元j个硬币的表示方法即为(i-1)元(j-1)个硬币+(i-5)元(j-1)个硬币+(i-10)元(j-1)个硬币+(i-25)元(j-1)个硬币+(i-50)元(j-1)个硬币的表示方法的总和。

我们要求的就算面值为i元的能用多少种硬币组成,即为第i行的所有列的和相加。

还要注意题目里说,硬币数不能超过100个。

发布了153 篇原创文章 · 获赞 4 · 访问量 3702

猜你喜欢

转载自blog.csdn.net/qq_39782006/article/details/103884743