[HDOJ]Coin Change(DP)

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2069

题意

有面值1,5,10,25,50的硬币数枚,对于输入的面值n,输出可凑成面值n(且限制总硬笔数小于等于100枚)的方案数。特别的,n=0时方案数=1。

其中,输入n<=250。

思路

DP。

状态 ways[j][i] 表示面值等于j且硬币枚数等于i时的方案数。

初始化时,只需将ways[0][0]=1即可,其他为0;

外层先遍历硬币面值种类,这层遍历的具体顺序不重要,即保证有不重复累加同样的组合即可;

中间层遍历硬币枚数,对从小到大硬币枚数,显然需正序;再遍历各种面值,这要正序,原理同完全背包;状态转移方程见代码。

最终输出答案时做相应的简单求和即可。

代码

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <set>

using namespace std;

#define MAX_MONEY 250
#define COIN_CNT 100

long long ways[MAX_MONEY+5][COIN_CNT+5];

int main(int argc, const char * argv[]) {
    set<int> coin={1,5,10,25,50};
    memset(ways, 0,sizeof(ways));
    ways[0][0]=1;

    for(auto it=coin.begin();it!=coin.end();++it){
        for(int i=1;i<=COIN_CNT;++i){
            for(int j=*it;j<=MAX_MONEY;++j){
                ways[j][i]+=ways[j-*it][i-1];
            }
        }
    }

    int money;
    while(~scanf("%d",&money)){
        long long ans=0;
        for(int i=0;i<=COIN_CNT;i++){
            ans+=ways[money][i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/coding-gaga/p/10372396.html

时间: 2024-11-05 04:48:15

[HDOJ]Coin Change(DP)的相关文章

LeetCode OJ 322. Coin Change DP求解

题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accepted: 15289 Total Submissions: 62250 Difficulty: Medium You are given coins of different denominations and a total amount of money amount. Write a func

HDU2069 Coin Change (DP)

题目意思: http://acm.hdu.edu.cn/showproblem.php?pid=2069 给你五种硬币:1,5,10,25,50,现在给出一个n,求出用用这些组成价值n的种类数,例如n=11: 1.11个1 2.1个10,1个1 3.1个5,6个1 4.2个5,1个1 特别注意:使用硬币数不能超过100,只要注意了这个就可以了. AC代码: /** *@xiaoran *dp[i],最多100枚硬币 */ #include<iostream> #include<cstdi

dp背包问题/01背包,完全背包,多重背包,/coin change算法求花硬币的种类数

一步一步循序渐进. Coin Change 具体思想:给你 N元,然后你有几种零钱S={S1,S2...,Sm} (每种零钱数量不限). 问:凑成N有多少种组合方式  即N=x1 * S1+x2*S2+...+xk*Sk (xk>=0,k=1,2..m) 设有f(x)中组合方式 有两种解答(自底向上回溯): 1.不用第m种货币   f(N,m-1) 2.用第m种货币 f(N-Sm,m) 总的组合方式为f(N,m)=f(N,m-1)+f(N-Sm,m) anything is nonsense,s

UVa 674 Coin Change (经典DP)

Coin Change Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu 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

HDU 2069 Coin Change (经典DP)

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14500    Accepted Submission(s): 4879 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1

UVa 674 &amp; hdu 2069 Coin Change (母函数,dp)

链接:uva 674 题意:有5中货币,价值分别为 50-cent, 25-cent, 10-cent, 5-cent,1-cent,数量都为无限个, 给定一个数 n,求用上述货币组成价值为 n 的方法有多少? 分析:因为n<=7489,可以用 母函数 或 dp 打表 对于dp状态方程为: dp[j]+=dp[j-c[i]] 母函数: #include<stdio.h> int c1[7500],c2[7500],w[5]={1,5,10,25,50};; void mhs() { in

uva 674 Coin Change 经典dp入门

Coin Change Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UVA 674 Appoint description: Description Download as PDF Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to m

[LeetCode][JavaScript]Coin Change

Coin Change You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination o

Leetcode OJ --- 322. Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins,