题目链接:https://www.nowcoder.com/practice/4e05294fc5aa4d4fa8eacef2e606e5a8?tpId=101&tqId=33080&tPage=1&rp=1&ru=/ta/programmer-code-interview-guide&qru=/ta/programmer-code-interview-guide/question-ranking

题目大意

略。

分析

完全背包问题,注意要填满背包。

代码如下

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3
  4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5 #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
  6 #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
  7 #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
  8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 12
 13 #define pr(x) cout << #x << " = " << x << "  "
 14 #define prln(x) cout << #x << " = " << x << endl
 15
 16 #define LOWBIT(x) ((x)&(-x))
 17
 18 #define ALL(x) x.begin(),x.end()
 19 #define INS(x) inserter(x,x.begin())
 20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
 21 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
 22 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
 23 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
 24
 25 #define ms0(a) memset(a,0,sizeof(a))
 26 #define msI(a) memset(a,0x3f,sizeof(a))
 27 #define msM(a) memset(a,-1,sizeof(a))
 28
 29 #define MP make_pair
 30 #define PB push_back
 31 #define ft first
 32 #define sd second
 33
 34 template<typename T1, typename T2>
 35 istream &operator>>(istream &in, pair<T1, T2> &p) {
 36     in >> p.first >> p.second;
 37     return in;
 38 }
 39
 40 template<typename T>
 41 istream &operator>>(istream &in, vector<T> &v) {
 42     for (auto &x: v)
 43         in >> x;
 44     return in;
 45 }
 46
 47 template<typename T>
 48 ostream &operator<<(ostream &out, vector<T> &v) {
 49     Rep(i, v.size()) out << v[i] << " \n"[i == v.size() - 1];
 50     return out;
 51 }
 52
 53 template<typename T1, typename T2>
 54 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 55     out << "[" << p.first << ", " << p.second << "]" << "\n";
 56     return out;
 57 }
 58
 59 inline int gc(){
 60     static const int BUF = 1e7;
 61     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 62
 63     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 64     return *bg++;
 65 }
 66
 67 inline int ri(){
 68     int x = 0, f = 1, c = gc();
 69     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
 70     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 71     return x*f;
 72 }
 73
 74 template<class T>
 75 inline string toString(T x) {
 76     ostringstream sout;
 77     sout << x;
 78     return sout.str();
 79 }
 80
 81 inline int toInt(string s) {
 82     int v;
 83     istringstream sin(s);
 84     sin >> v;
 85     return v;
 86 }
 87
 88 //min <= aim <= max
 89 template<typename T>
 90 inline bool BETWEEN(const T aim, const T min, const T max) {
 91     return min <= aim && aim <= max;
 92 }
 93
 94 typedef unsigned int uI;
 95 typedef long long LL;
 96 typedef unsigned long long uLL;
 97 typedef vector< int > VI;
 98 typedef vector< bool > VB;
 99 typedef vector< char > VC;
100 typedef vector< double > VD;
101 typedef vector< string > VS;
102 typedef vector< LL > VL;
103 typedef vector< VI > VVI;
104 typedef vector< VB > VVB;
105 typedef vector< VS > VVS;
106 typedef vector< VL > VVL;
107 typedef vector< VVI > VVVI;
108 typedef vector< VVL > VVVL;
109 typedef pair< int, int > PII;
110 typedef pair< LL, LL > PLL;
111 typedef pair< int, string > PIS;
112 typedef pair< string, int > PSI;
113 typedef pair< string, string > PSS;
114 typedef pair< double, double > PDD;
115 typedef vector< PII > VPII;
116 typedef vector< PLL > VPLL;
117 typedef vector< VPII > VVPII;
118 typedef vector< VPLL > VVPLL;
119 typedef vector< VS > VVS;
120 typedef map< int, int > MII;
121 typedef unordered_map< int, int > uMII;
122 typedef map< LL, LL > MLL;
123 typedef map< string, int > MSI;
124 typedef map< int, string > MIS;
125 typedef set< int > SI;
126 typedef stack< int > SKI;
127 typedef deque< int > DQI;
128 typedef queue< int > QI;
129 typedef priority_queue< int > PQIMax;
130 typedef priority_queue< int, VI, greater< int > > PQIMin;
131 const double EPS = 1e-8;
132 const LL inf = 0x7fffffff;
133 const LL infLL = 0x7fffffffffffffffLL;
134 const LL mod = 1e9 + 7;
135 const int maxN = 1e3 + 7;
136 const LL ONE = 1;
137 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
138 const LL oddBits = 0x5555555555555555;
139
140 int N, aim, arr[maxN];
141 // dp[i][j]表示前 i 种货币,组成 j 元,需要的最少货币数
142 int dp[maxN * 5];
143
144 int main(){
145     //freopen("MyOutput.txt","w",stdout);
146     //freopen("input.txt","r",stdin);
147     //INIT();
148     scanf("%d%d", &N, &aim);
149     For(i, 1, N) scanf("%d", &arr[i]);
150     dp[0] = 1;
151
152     For(i, 1, N) {
153         For(j, arr[i], aim) {
154             if(!dp[j - arr[i]]) continue;
155             if(!dp[j]) dp[j] = dp[j - arr[i]] + 1;
156             else dp[j] = min(dp[j], dp[j - arr[i]] + 1);
157         }
158     }
159
160     printf("%d\n", dp[aim] - 1);
161     return 0;
162 }

View Code

转载于:https://www.cnblogs.com/zaq19970105/p/11372297.html

牛客 换钱的最少货币数相关推荐

  1. 牛客题霸 [ 换钱的最少货币数] C++题解/答案

    牛客题霸 [ 换钱的最少货币数] C++题解/答案 题目描述 给定数组arr,arr中所有的值都为正整数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个aim,代表要找的钱 ...

  2. Python换钱的最少货币数

    题目: 给定数组arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim,代表要找的钱数,求组成aim的最少货币数. 例: arr = [ ...

  3. 最少钱币数不java,【动态规划专题】3:换钱的最少货币数

    <程序员代码面试指南--IT名企算法与数据结构题目最优解> 左程云 著 换钱的最少货币数 [题目] 给定数组arr, arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的 ...

  4. java 最少货币单元组合换钱_动态规划. 换钱的最少货币数和最多方法数

    通过对换钱类题目的学习,我们将了解到 暴力递归及优化方法 记忆搜索(优化一) 动态规划的基本实现方法(优化二) 动态规划的空间优化(优化三) 1. 换钱的最少货币数,货币可重复使用 给定数组arr,a ...

  5. 换钱的最少货币数(NC126/考察次数Top69/难度简单)

    描述: 给定数组arr,arr中所有的值都为正整数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个aim,代表要找的钱数,求组成aim的最少货币数. 如果无解,请返回-1. ...

  6. python 货币合适_算法之Python实现 - 001 : 换钱的最少货币数

    [题目]给定数组arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim代表要找的钱数,求组成aim的最少货币数. [代码1]:时间与额外 ...

  7. 动态规划问题——换钱的最少货币数

    题目: 给定数组arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,在给定一个整数aim,代表要找的钱数,求组成aim的最少货币数. 示例: arr = ...

  8. 算法:换钱的最少货币数

    题目 给定数组arr,arr中所有的值都为正整数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个aim,代表要找的钱数,求组成aim的最少货币数. 输入 输入包括两行,第一 ...

  9. [动态规划] 换钱的最少货币数

    算法专题导航页面 [题目描述]     给定数组arr,arr中所有的值都为正整数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个aim,代表要找的钱数,求组成aim的最少 ...

最新文章

  1. Android面试收集录12 View测量、布局及绘制原理
  2. boost::gil::pixels_are_compatible用法的测试程序
  3. [转载]Javascript异步编程的4种方法
  4. cf1556Compressed Bracket Sequence
  5. C++工程师面试题大全
  6. 全局路径规划:图搜索算法介绍3(A stars tie breaker)
  7. 在继续之前,如何暂停我的Shell脚本一秒钟?
  8. 第4章 同步控制 Synchronization ----同步机制的摘要
  9. 性能测试负载模型(十)
  10. java项目相关架构图
  11. 什么是 PaaS?“平台即服务“ 简介
  12. kuwo.php采集,PHP获取酷我音乐MP3外链
  13. 台式计算机可以连接蓝牙吗,蓝牙耳机怎么连接台式电脑?台式电脑连接蓝牙耳机的方法...
  14. java Excel导出功能之 固定列表格
  15. HDU 4544 湫湫系列故事——消灭兔子 (贪心+优先队列)
  16. excel表如何汇总统计平均值
  17. Pic16f1828 1829串口收发
  18. 2014年CCNU-ACM暑期集训总结
  19. 电影管理软件+android,影视多媒体管理软件Media Companion
  20. 商务快车软件测试大乐,商务快车这个软件有用过的吗?效果怎么样? 爱问知识人...

热门文章

  1. 浅析高层建筑消防安全难点 提出防控措施意见
  2. 关于瑕点型反常积分的收敛性判别
  3. 反常积分(Anomalous Integrals)
  4. 全球BT下载网站排名
  5. 公众号内容拓展学习笔记(2021.5.1)
  6. 美术集速写模特姿势参考大全,又酷又飒的姿势你画过了吗?
  7. Java练习题第十九期:另类加法
  8. imoo c1语言设置在哪里,imoo C1值得买?步步高学习手机imoo C1全面深度评测图解
  9. 李俊峰:碳达峰、碳中和决策不是心血来潮,2030年碳达峰不是一件特别难的事
  10. 【LeetCode】263.丑数 264. 丑数 II