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. MySQL UTF8 转为 utf8mb4

    https://mathiasbynens.be/notes/mysql-utf8mb4#utf8-to-utf8mb4 How to support full Unicode in MySQL da ...

  2. learning java AWT Pannel

    import java.awt.*; public class PanelTest { public static void main(String[] args) { var f = new Fra ...

  3. Linux系统性能10条命令

    概述 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat - ...

  4. Excel、CSV文件处理

    1.Excel中以\t做为列分隔符,换行符作为行分隔符 使用c#导出excel的时候,当数字太长时,如身份证号,导出后的excel就会显示为科学计数法.如“511122154712121000”会显示 ...

  5. AtCoder Grand Contest 030 (AGC030) F - Permutation and Minimum 动态规划

    原文链接www.cnblogs.com/zhouzhendong/p/AGC030F.html 草率题解 对于每两个相邻位置,把他们拿出来. 如果这两个相邻位置都有确定的值,那么不管他. 然后把所有的 ...

  6. C 库函数 - strcspn()

    定义 size_t strcspn(const char *str1, const char *str2) 参数 str1 -- 要被检索的 C 字符串. str2 -- 该字符串包含了要在 str1 ...

  7. SQL题(子文章)(持续更新)

    -----> 总文章 入口 文章目录 [-----> 总文章 入口](https://blog.csdn.net/qq_37214567/article/details/90174445) ...

  8. window.location.href在微信端不起作用的解决方法?

    在我从第一张图的某个活动进去到详情页,点击返回是可以的,这里我是用了一个click事件,window.location.href="某死链接" 但是第二次进去点击之后点击事件是可以 ...

  9. git命令note

    日志查看 git log 太乱? git log --pretty=oneline 版本回退 git reset --hard commit_id git reset --hard HEAD^ 上上版 ...

  10. H.264与H.265视频压缩编码参考码率