钱币问题 DP

// 动态规划求最少钱币枚数
#include <stdio.h>
int a[20],c[20][3000],k,n;
int min(int x,int y)
{ return x>y?y:x; }
int main()
{ int i,j;
scanf("%d",&k);
for (i=0; i<k; i++) scanf("%d",&a[i]);
scanf("%d",&n);
for (j=0; j<=n; j++) c[0][j]=j;
for (i=1;i<k; i++)
for (j=0;j<=n;j++)
if ( j>=a[i] )
c[i][j]=min( c[i-1][j], c[i][j-a[i]]+1);
else c[i][j]=c[i-1][j];
printf("%d\n",c[k-1][n]);
}

钱币问题 DP,布布扣,bubuko.com

时间: 2024-10-15 06:57:26

钱币问题 DP的相关文章

2014年百度之星程序设计大赛 - 资格赛 1004 -- Labyrinth

Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1453    Accepted Submission(s): 500 Problem Description 度度熊是一只喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫只能从矩阵左上角第一个方格开始走,只有走到右上角的第一个格子才算走出迷宫,每一次只能走一格,

Charlie&#39;s Change POJ - 1787 (完全背包+记录路径)

完全背包输出路径:对于每一次更新记录一下路径:注意钱币个数: dp[i][0]代表在空间为i时需要多少枚钱币 dp[i][1]用来记录路径 cheek[j]用来记录在j时用了多少i枚钱币 思路在代码中: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define mem(a,b) memset(a,b,sizeof

hdu 1284 钱币兑换问题 (递推 || DP || 母函数)

钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5069    Accepted Submission(s): 2868 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Outpu

HDU 1284 钱币兑换问题 母函数、DP

题目链接:HDU 1284 钱币兑换问题 钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5467    Accepted Submission(s): 3123 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有

BZOJ 3233: [Ahoi2013]找硬币( dp )

dp(x)表示最大面值为x时需要的最少硬币数. 枚举x的质因数p,  dp(x) = min( dp(x/p) - (p-1) * sigma[a[i]/x] ). ---------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> using namespace

BZOJ 1021: [SHOI2008]Debt 循环的债务( dp )

dp(i, j, k)表示考虑了前i种钱币(从小到大), Alice的钱数为j, Bob的钱数为k, 最小次数. 脑补一下可以发现, 只有A->B.C, B->A.C, C->A.B, A.B->C, A.C->B, B.C->A 6情况, 枚举然后dp一下就OK了. dp用刷表的话,有个强有力的剪枝是之后的硬币无论如何组合都无法满足时不去更新. --------------------------------------------------------------

HDU-1114(背包DP)

Piggy-Bank Problem Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM

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

DP(正解完全背包+容斥)

DP Time Limit:10000MS     Memory Limit:165888KB     64bit IO Format:%lld & %llu Submit Status Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s,其中di,s<=100000,