UVA 674 Coin Change 硬币转换(完全背包,常规)

题意:有5种硬币,个数无限的,组成n元的不同方案有多少种?

思路:常规完全背包。重点在dp[0]=1,dp[j]中记录的是组成 j 元的方案数。状态转移方程dp[j+coin[i]]+=dp[j]。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int coin[]={1, 5, 10, 25, 50};
 4 int dp[10000], n;
 5
 6 int cal()
 7 {
 8     if(!n)  return 1;
 9     memset(dp,0,sizeof(dp));
10     dp[0]=1;
11     for(int i=0; i<5; i++)
12         for(int j=0; j+coin[i]<=n; j++)
13             dp[j+coin[i]]+=dp[j];
14     return dp[n];
15 }
16 int main() {
17     //freopen("input.txt", "r", stdin);
18     while(~scanf("%d",&n))
19         printf("%d\n",cal());
20
21     return 0;
22 }

AC代码

时间: 2024-08-05 19:31:29

UVA 674 Coin Change 硬币转换(完全背包,常规)的相关文章

uva 674 Coin Change(类似完全背包)

有点类似完全背包,不过最后的容量必须被充满. dp[i][j]表示在前i个物品中选择容量不超过j的最大价值. 完全背包转移方程:dp[i][j] = max(dp[i-1][j] , dp[i][j-v[i]]+w[i]) 这道题目设数组dp[i][j]表示用前j个硬币组成i的种类个数,转移方程:dp[i][j] = dp[i-1][j]+dp[i][j- a[i]]因为这里求得是解的个数,所以要用加法,完全背包是求某一种情况所以去最大的.(明天实现一下一维数 组) 代码: #include<i

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

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

UVa 674 Coin Change(完全背包)

https://vjudge.net/problem/UVA-674 题意: 计算兑换零钱的方法共有几种. 思路: 完全背包基础题. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 int d[7500]; 8 int a[5] = { 1, 5, 10, 25, 50 }; 9 10 in

UVA 674 Coin Change

DP,背包的思想. 问 最多7489块钱.有多少种组成方式.面额分别为 1,5,10,25,50: 由于不限制硬币数量,所以完全背包,累加就可以了. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream

UVa 674 Coin Change【记忆化搜索】

题意:给出1,5,10,25,50五种硬币,再给出n,问有多少种不同的方案能够凑齐n 自己写的时候写出来方案数老是更少(用的一维的) 后来搜题解发现,要用二维的来写 http://blog.csdn.net/keshuai19940722/article/details/11025971 这一篇说的是会有面值的重复问题,还不是很理解 还有就是一个预处理的问题, 看了题解之后再自己写,很习惯的把处理dp数组写到while循环里面,一直tle 后来看到这篇题解 http://www.cnblogs.

UVA 674 Coin Change (DP)

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 c

[LeetCode] 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,

HDU-2016 Coin Change (母函数 | 多重背包)

题意:给你多组数据,然后给出一个面额n,已知有5种钱币1, 5 ,10 , 25, 50求可以组成n元的可能数 (同时所花费的钱币个数要小于等于100) 思路:从多重背包来理解,即使每个硬币占一个单位空间,有100个空间通过状态转移方程:dp[j][k] += dp[j-v[i]][k-1]; 这里dp记录的便是组合的种类数(相当于递推公式, 从dp[0][0]这个状态开始,尝试放入不同的硬币并且符合条件) 总母函数的角度:同样相当于取5种类型的硬币,然后通过母函数性质模拟多项式乘积 便可以得到