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. ML,DL核心数学及算法知识点总结

    ML,DL核心数学及算法知识点总结:https://mp.weixin.qq.com/s/bskyMQ2i1VMNiYKIvw_d7g

  2. ERROR: `elasticsearch` directory is missing in the plugin zip

    该问题出现在为elasticsearch安装中文分词器插件时 问题发生在插件和es版本不匹配~ 解决: es版本与插件版本对应齐 命令行安装 C:\Users\SeeClanUkyo>F:\el ...

  3. 洛谷P1162(自我感觉思路还算巧妙的一道题)

    P1162 填涂颜色 题目描述 由数字0 组成的方阵中,有一任意形状闭合圈,闭合圈由数字1构成,围圈时只走上下左右4个方向.现要求把闭合圈内的所有空间都填写成2.例如:6X6的方阵(n=6),涂色前和 ...

  4. python 之 列表常用 操作

  5. 数据结构实验之排序五:归并求逆序数(SDUT 3402)

    归并排序详解(戳我). 以下是搬了别人的. #include<stdio.h> #include<stdlib.h> long long sum = 0; int a[1000 ...

  6. deepin 深度Linux系统 15.11 链接蓝牙鼠标问题

    不知道为毛就是搜索不到,好吧只能用老方法,那就是不使用deepin系统自带的面板进行管理 用下面的命令进行安装配置即可 sudo apt install bluetooth blueman bluem ...

  7. Git的使用(4) —— 分支的概念和使用

    1. 概念 在SVN中,分支并不是很便于使用.但是在Git中,分支就变成了特别好用的功能呢,受到大多数使用者的青睐. 分支中有几个概念: (1) 分支:分支就是每一次提交创建的点连接成的线. (2) ...

  8. cookie 的HttpOnly 和 Secure 属性

    设置HttpOnly=true的cookie不能被js获取到,无法用document.cookie打出cookie的内容. Secure属性是说如果一个cookie被设置了Secure=true,那么 ...

  9. SELECT INTO (insert into) 和 INSERT INTO SELECT (insert into select) 两种表复制语句

    1.INSERT INTO SELECT语句   语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from T ...

  10. [转] FileZilla Server超详细配置

    FileZilla Server下载安装完成后,必须启动软件进行设置,由于此软件是英文,本来就是一款陌生的软件,再加上英文(注:本站提供中文版本,请点击下载),配置难度可想而知,站长从网上找到一篇非常 ...