POJ
来源:互联网 发布:fanuc数控系统编程入门 编辑:程序博客网 时间:2024/06/14 01:03
题意:你有1,5,10,25四种硬币数量有限,你想买价格为p的咖啡,问你能不能购买,要求花的硬币尽量多,并且输出每种硬币花了多少;
思路:看起来像多重背包加记录路径但用完全背包比较好写
#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<map>#include<queue>#include<cmath>#include<stack>#include<vector>#include<cstdio>#define MAXN 33000#define INF 0x3f3f3f3f#define lmid l,m,rt<<1#define rmid m+1,r,rt<<1|1#define ls rt<<1#define rs rt<<1|1#define Mod 1000000007#define i64 __int64#define LIMIT_ULL 100000000000000000using namespace std;int dp[10005];int a[10005][4];int s[4];int t[4]={1,5,10,25};int main(){ int n; while(scanf("%d",&n)==1) { memset(dp,-1,sizeof(dp)); memset(a,0,sizeof(a)); for(int i=0;i<4;i++) { scanf("%d",&s[i]); } if(n==0&&s[0]==0&&s[1]==0&&s[2]==0&&s[3]==0) break; dp[0]=0; for(int i=0;i<4;i++) for(int j=t[i];j<=n;j++) { if(dp[j-t[i]]!=-1) { if(dp[j]<dp[j-t[i]]+1&&a[j-t[i]][i]+1<=s[i]) { dp[j]=dp[j-t[i]]+1; for(int k=0;k<4;k++) a[j][k]=a[j-t[i]][k]; a[j][i]++; } } } if(dp[n]==-1) { cout<<"Charlie cannot buy coffee."<<endl; continue; } printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",a[n][0],a[n][1],a[n][2],a[n][3]); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- HBase特征
- To The Max
- Mysql字段的增删改查
- 路由表
- [UESTC 1583]曜酱的心意
- POJ
- 深入分析Spring 与 Spring MVC容器启动
- 《利用Python进行数据分析》学习笔记ch05(6)
- HDU 1823 Luck and Love 二维线段树
- 由jdbc的入门程序学到的Java程序执行过程--class.forname()
- Shibboleth与SAML 2.0的关系
- ZOJ 3519 Who is the Smartest Man (贪心)
- JavaScript中this、apply()、call()的用法及解释示例
- 笔试题8——二进制中1的个数