[CareerCup] 9.8 Represent N Cents 组成N分钱
9.8 Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), write code to calculate the number of ways of representing n cents.
给定一个钱数,用quarter,dime,nickle和penny来表示的方法总和。
Java:
public class Solution {/*** @param n an integer* @return an integer*/public int waysNCents(int n) {int[] f = new int[n+1];f[0] = 1;int[] cents = new int[]{1, 5, 10, 25};for (int i = 0; i < 4; i++) for (int j = 1; j <= n; j++) {if (j >= cents[i]) {f[j] += f[j-cents[i]];}}return f[n];}
}
Python:
class Solution:# @param {int} n an integer# @return {int} an integerdef waysNCents(self, n):# Write your code herecents = [1, 5, 10, 25]ways = [0 for _ in xrange(n + 1)]ways[0] = 1for cent in cents:for j in xrange(cent, n + 1):ways[j] += ways[j - cent]return ways[n]
C++:
class Solution {
public:/*** @param n an integer* @return an integer*/int waysNCents(int n) {// Write your code herevector<int> cents = {1, 5, 10, 25};vector<int> ways(n + 1);ways[0] = 1;for (int i = 0; i < 4; ++i)for (int j = cents[i]; j <= n; ++j)ways[j] += ways[j - cents[i]];return ways[n];}
};
C++:
class Solution {
public:int makeChange(int n) {vector<int> denoms = {25, 10, 5, 1};vector<vector<int> > m(n + 1, vector<int>(denoms.size()));return makeChange(n, denoms, 0, m);}int makeChange(int amount, vector<int> denoms, int idx, vector<vector<int> > &m) {if (m[amount][idx] > 0) return m[amount][idx];if (idx >= denoms.size() - 1) return 1;int val = denoms[idx], res = 0;for (int i = 0; i * val <= amount; ++i) {int rem = amount - i * val;res += makeChange(rem, denoms, idx + 1, m);}m[amount][idx] = res;return res;}
};
类似题目:
[LeetCode] 322. Coin Change 硬币找零
CareerCup Questions List 职业杯题目列表
转载于:https://www.cnblogs.com/lightwindy/p/8674187.html
[CareerCup] 9.8 Represent N Cents 组成N分钱相关推荐
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目8
2014-03-20 04:04 题目:给你不限量的1分钱.5分钱.10分钱.25分钱硬币,凑成n分钱总共有多少种方法? 解法:理论上来说应该是有排列组合的公式解的,但推导起来太麻烦而且换个数据就又得 ...
- 第二章第二十二题(金融应用:货币单位)(Financial application: monetary units)
*2.22(金融应用:货币单位)改写程序清单2-10,解决将double型值转换为int型值可能会造成精度损失问题.以整数值作为输入,其最后两位代表的是美分币值.例如:1156就表示的是11美元56美 ...
- 【cc150】 硬币问题
一.题目 硬币问题: Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and ...
- 英语语法最终珍藏版笔记-10动名词
动名词 动名词可以起名词的作用,在句子中作主语,宾语,表语,定语.动名词仍保留动词的一般特征,可以有自己的宾语和状语,构成动名词短语. 动名词的否定形式是在前面加上not, never等否定词. (一 ...
- [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序
11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...
- [CareerCup] 17.7 English Phrase Describe Integer 英文单词表示数字
17.7 Given any integer, print an English phrase that describes the integer (e.g., "One Thousand ...
- [CareerCup] 17.6 Sort Array 排列数组
17.6 Given an array of integers, write a method to find indices m and n such that if you sorted elem ...
- Careercup - Google面试题 - 4699414551592960
2014-05-06 13:34 题目链接 原题: we have a random list of people. each person knows his own height and the ...
- Careercup | Chapter 1
1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...
- [CareerCup] 15.5 Denormalization 逆规范化
15.5 What is denormalization? Explain the pros and cons. 逆规范化Denormalization是一种通过添加冗余数据的数据库优化技术,可以帮助 ...
最新文章
- Spark采坑系列(三)Spark操作Hive的坑
- npm包实现发布正式和测试版
- 【CDN】域名无法访问,ping不到,tracert不到
- 如何进行自动化PDF测试?这里有真正简单明了的方法
- 引入方式之内部样式表(CSS、HTML)
- mysql切换二进制日志命令_Mysql二进制日志binlog命令
- cuda对应pytorh安装
- 联想笔记本修复计算机还原系统失败,联想笔记本电脑重装系统不成功,联想笔记本系统恢复...
- c语言中stdio什么意思6,求助,return,stdio是什么意思,在C语言中又是什么意思
- Android MTK修改手机型号
- Linux ——IP配置修改
- Java大型工程项目管理系统源码,原生APP源码,建筑工程管理源码
- fig r函数_R语言图形函数par()参数应用举例.pdf
- python选题背景_选题背景及课题研究的目的与意义范例
- 虚拟机:TTL 传输中过期
- Obsidian基本使用
- java获取当前时间的前一个小时
- 圣诞节,1inch狂撒3亿美金红包,币圈大佬在线炫富,我酸了...
- 使用appium进行app自动化测试时遇到AppActivity设置正确但报Connect Appium Server Fail.A new session could not be created
- 解决因为微信字体过大导致样式变乱的问题
热门文章
- 长沙动环监控系统主要监测哪些指标呢?
- 网站被劫持的方式,和检测方法、网站被劫持、检测方法有哪些
- 用 Amazon Web Services 进行云计算,第 3 部分: 用 EC2 根据需要提供服务器
- Git 工具之储藏与清理-7.3
- MMO 游戏服务器引擎设计
- 360屏保广告彻底关闭教程
- D. Bouncing Boomerangs
- 高通平台android9.0充电电量,充电指示灯以及充电图标读取分析
- python与数据挖掘 分类和预测
- html 字体样式引用,CSS3 关于@font-face引用中文字体解决办法