思路:

完全背包,记录路径。

实现:

 #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int v[] = {, , , };
int c[], m;
int dp[], used[], path[];
int main()
{
while (cin >> m >> c[] >> c[] >> c[] >> c[], m || c[] || c[] || c[] || c[])
{
memset(used, , sizeof used);
memset(path, , sizeof path);
path[] = -;
for (int i = ; i <= m; i++) dp[i] = -INF;
dp[] = ;
for (int i = ; i < ; i++)
{
memset(used, , sizeof used);
for (int j = v[i]; j <= m; j++)
{
if (dp[j - v[i]] != -INF && dp[j - v[i]] + > dp[j] && used[j - v[i]] < c[i])
{
dp[j] = dp[j - v[i]] + ;
used[j] = used[j - v[i]] + ;
path[j] = j - v[i];
}
}
}
if (dp[m] <= -INF) { cout << "Charlie cannot buy coffee." << endl; }
else
{
int ans[]; memset(ans, , sizeof ans);
while (path[m] != -)
{
ans[m - path[m]]++;
m = path[m];
}
cout << "Throw in " << ans[v[]] << " cents, " << ans[v[]] << " nickels, " << ans[v[]] << " dimes, and " << ans[v[]] << " quarters." << endl;
}
}
return ;
}

poj1787 Charlie's Change的更多相关文章

  1. poj1787 Charlie's Change

    Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he ofte ...

  2. (多重背包+记录路径)Charlie's Change (poj 1787)

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  3. Charlie's Change(完全背包+路径记忆)

    Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3176   Accepted: 913 D ...

  4. Charlie's Change(完全背包记录路径)

    Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...

  5. Charlie's Change POJ - 1787

    Time limit 1000 ms Memory limit 30000 kB description Charlie is a driver of Advanced Cargo Movement, ...

  6. poj 1787 Charlie's Change (多重背包可作完全背包)

    Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3792   Accepted: 1144 ...

  7. B - Charlie's Change

    Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...

  8. [POJ 1787]Charlie's Change (动态规划)

    题目链接:http://poj.org/problem?id=1787 题意:有4种货币分别是1元,5元,10元,20元.现在告诉你这四种货币分别有多少个,问你正好凑出P元钱最多可以用多少货币.每种货 ...

  9. POJ 1787 Charlie's Change (完全背包/多重背包,输出方案的物品个数)

    网上说是多重背包,因为要输出方案,还要记录下路径,百度一下题解就可以. 自己做的时候,还没了解过多重背包,该题直接往完全背包思考了.咖啡的钱看作总的背包容量,1.5.10.25分别代表四种物品的重量, ...

随机推荐

  1. JAVA初级复习知识点归纳

    JDK的安装: 下载.安装 配置环境变量 a) path:.;%JAVA_HOME%\bin; b) JAVA_HOME:JDK的安装目录 c) classpath JDK和JRE和JVM: JAVA ...

  2. Servlet实现点击计数器

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/hits-counter.html: 一.Web页面的点击计数器 很多时候,可能有兴趣知道网站的某 ...

  3. mysql你确定掌握的那些sql语句

    1.创建表 create table test(uid int not null,create_time timestamp default current_timestamp); 即:没有双引号,单 ...

  4. MapReduce获取分片数目

    问题 MapReduce Application中mapper的数目和分片的数目是一样的,可是分片数目和什么有关呢? 默认情况下.分片和输入文件的分块数是相等的.也不全然相等,假设block size ...

  5. Linux学习日志--文件搜索命令

    开头总结: 学习了Linux中的文件搜索命令find和locate,系统搜索命令whereis 和which ,字符串搜索命令grep,find和locate的差别和使用方法格式,什么是path环境变 ...

  6. ImportError: No module named &#39;ConfigParser&#39;

    Resolve Method: I found the problem. I had manually installed a newer version of python (version 3.2 ...

  7. js 返回顶部

    <script> window.onload = function(){ var oTop = document.getElementById("to_top"); v ...

  8. REST技术第二步 获取URL中的參数

    获取请求的參数.rest技术相对于servlet来说要方便很多. Servlet我们要获取请求的參数,非常麻烦啊.须要request.getParameter("").假设我们要的 ...

  9. 抽象类(abstract class)和接口(interface)有什么异同?

    相同点: 1.抽象类和接口都不能被实例化,但可以定义抽象类和接口类型的引用. 2.一个类如果继承了抽象类和接口,必须要对其中的抽象方法全部实现.(接口中方法默认的是public abstract修饰的 ...

  10. ios31--NSThread

    // // ViewController.m // 03-掌握-NSThread基本使用 #import "ViewController.h" #import "XMGT ...