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. CSS3中的display:grid网格布局介绍

    1.网格布局(grid): 它将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局; 2.基本概念: 容器和项目,如图所示: <div class="content&qu ...

  2. mysql模糊查询多个字段

    SELECT * FROM nst_t_conferencehis WHERE ConferName REGEXP '军工|军资';

  3. 19、Executor原理剖析与源码分析

    一.原理图解 二.源码分析 1.Executor注册机制 worker中为Application启动的executor,实际上是启动了这个CoarseGrainedExecutorBackend进程: ...

  4. DirectX学习入门笔记(一)

    原文:https://blog.csdn.net/butcher986115/article/details/50595937  什么是DirectX? DirectX是游戏制作者的API(Appli ...

  5. python2 && python3 的 input函数

    Python2.x中的input()函数input()函数让我们明确我们输入的是数字格式还是字符格式,就是我们自己要知道我们想要的是什么,数字格式直接输入,字符格式必须加上单引号或者双引号,以确定我们 ...

  6. 2、Apache(httpd)之一 三种工作模式

    httpd的特性: 高度模块化:core + modules 模块化设计DSO:Dynamic Shared Object MPM:Multipath Processing Modules 多路处理模 ...

  7. Java ArrayList几种遍历方法

    import java.util.ArrayList; import java.util.Iterator; public class StringSampleDemo { public static ...

  8. 小程序的基本原生js使用

    1.点击事件 <a data-current="{{setting.current}}" bindtap="clickcurrent" style=&qu ...

  9. Redis哨兵日志说明

    一.说明

  10. 使用IDEA创建一个Maven Web工程:无法创建Java Class文件

    今天用IDEA新建了一个maven web工程,项目目录是这样的: 在新创建一个Java class 文件时,却没有Java class功能,无法创建,如图: 解决方案: 选择 File——>P ...