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<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<cmath>
#define LL long long
using namespace std;
const int a[5]={1,5,10,25,50};
LL dp[255][101];//dp[j][k]:用k个硬币组成j值的个数
int main()
{
    int n;
    while(cin>>n){
        //cout<<res[n]<<endl;
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=0;i<5;i++){
            for(int k=1;k<=100;k++){//k个硬币
                for(int j=a[i];j<=n;j++){
                    dp[j][k]+=dp[j-a[i]][k-1];
                }
            }
        }
        int res=0;
        for(int i=0;i<=100;i++){
            res+=dp[n][i];
        }
        cout<<res<<endl;
    }
	return 0;
}
时间: 2024-10-12 10:14:50

HDU2069 Coin Change (DP)的相关文章

HDU2069 Coin Change 【暴力】

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

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

[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: 外层先遍历硬币面值种类,这层遍历的具体顺序不重要,即保证有不重复累加同

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

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

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