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 integer
def waysNCents(self, n):
# Write your code here
cents = [1, 5, 10, 25]
ways = [0 for _ in xrange(n + 1)] ways[0] = 1
for 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 here
vector<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 职业杯题目列表

[CareerCup] 9.8 Represent N Cents 组成N分钱的更多相关文章

  1. [CareerCup] 9.8 Represent N Cents 美分的组成

    9.8 Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies ...

  2. [LeetCode] 518. Coin Change 2 硬币找零 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  3. CareerCup Questions List 职业杯题目列表

    网站 www.careercup.com 上的题库列表 # Title Difficulty Company 1 Guards in a museum Hard F, G  2 Bomberman H ...

  4. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  5. [CareerCup] 9.10 Stack Boxes 垒箱子问题

    9.10 You have a stack of n boxes, with widths w., heights hir and depths drThe boxes cannot be rotat ...

  6. CareerCup All in One 题目汇总

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  7. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  8. [LeetCode] Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  9. Leetcode 322.零钱兑换

    零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: co ...

随机推荐

  1. Java【基础学习】 之调用构造方法顺序【坑】

    解释:这里的super()仅仅是用来占位的,实际上,必须是严格按照分层初始化的过程:1.先初始化父类X的成员变量,即初始化成员变量Y,打印出:Y2.初始化父类X的构造方法,打印出:X3.父类初始化完成 ...

  2. django 第五天 自定义标签 静态文件

    昨日忘记上传,先预留位置,稍后补上

  3. C# 遍历 enum 枚举

    foreach (Suit suit in (Suit[]) Enum.GetValues(typeof(Suit))) { } 注意:强制转换(Suit[])不是必需的,但确实使代码快0.5 ns.

  4. YAML_05 定义一个变量创建用户,设置密码

    ansible]# vim user2.yml --- - hosts: cache   remote_user: root   vars:     user: wangwu   tasks:     ...

  5. BCB key事件中判断Shift、Alt、Ctrl状态

    BCB key事件中判断Shift.Alt.Ctrl状态: 类似此事件中 void __fastcall TForm1::keydown(TObject *Sender, WORD &Key, ...

  6. CSS3新增选择器:伪元素选择器

    一.  ::first-letter 第一个字 二. ::first-line 第一行(以浏览器为准的第一行) 三. ::selection 被选中的字行(鼠标选中的字段)只能向 ::selectio ...

  7. cf 1037D BFS

    $des$一个 n 个点 m 条边的无向连通图从 1 号点开始 bfs,可能得到的 bfs 序有很多,取决于出边的访问顺序.现在给出一个 1 到 n 的排列,判断是否可能是一个 bfs 序. $sol ...

  8. Bzoj 2154: Crash的数字表格(积性函数)

    2154: Crash的数字表格 Time Limit: 20 Sec Memory Limit: 259 MB Description 今天的数学课上,Crash小朋友学习了最小公倍数(Least ...

  9. P1453 城市环路

    题目背景 一座城市,往往会被人们划分为几个区域,例如住宅区.商业区.工业区等等.B市就被分为了以下的两个区域——城市中心和城市郊区.在着这两个区域的中间是一条围绕B市的环路,环路之内便是B市中心. 题 ...

  10. Codeforces 1172E Nauuo and ODT [LCT]

    Codeforces ZROI那题是这题删掉修改的弱化版--ZROI还我培训费/px 思路 按照套路,我们考虑每种颜色的贡献,然后发现不包含某种颜色的路径条数更容易数,就是删掉该颜色的点后每个连通块大 ...