9.8---硬币问题(CC150)
这道题卡了一天。要想AC非常难。
1,第一个解决办法,优化暴力:
public class Coins {
public static int countWays(int n){
int num25 = n / 25;
long res = 0;
for(int i = 0; i <= num25;i++){
int leave25 = n - i * 25;
int num10 = leave25 / 10;
for(int j = 0; j <= num10; j++){
int leave10 = leave25 - j * 10;
int num5 = leave10 / 5;
res += num5 + 1;
res = res % 1000000007;
}
}
return (int) res;
}
}
2,第二个解决办法,递推式:
但是LTE。
dp[i][sum] = 用前i种硬币构成sum 的所有组合数。
http://www.cnblogs.com/python27/archive/2013/09/05/3303721.html
public static int myCountWays(int n){
int[][] dp = new int[5][n+1];
int[] coins = {1,5,10,25};
for(int i = 0; i <= 4; i++){
dp[i][0] = 1;
}
for (int i = 1; i <= 4; ++i)
{
for (int j = 1; j <= n; ++j)
{
dp[i][j] = 0;
for (int k = 0; k <= j / coins[i-1]; ++k)
{
dp[i][j] += dp[i-1][j - k * coins[i-1]];
}
}
}
return dp[4][n];
}
3,最好的答案:
public static int countWays(int n) {
// write code here
int[] coins={1,5,10,25};
int[] dp = new int[100001];
dp[0] = 1;
for(int i = 0;i < 4;++i){
for(int j = coins[i];j <= n;++j){
dp[j] =(dp[j]+dp[j-coins[i]])%1000000007;
}
}
return dp[n];
}
目前的理解是:
如果只有面值1,那么所有值都是1.
如果有两种面值1,5.那么dp[i] = dp[i] + dp[i - 5];从5开始算。
所以。
9.8---硬币问题(CC150)的更多相关文章
- [cc150] 硬币问题
Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 面试题目——《CC150》递归与动态规划
面试题9.1:有个小孩正在上楼梯,楼梯有n个台阶,小孩一次可以上1阶.2阶或者3阶.实现一个方法,计算小孩有多少种上楼梯的方式. 思路:第4个数是前三个数之和 注意:能不能使用递归,能不能建立一个很大 ...
- [LeetCode] Arranging Coins 排列硬币
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- 【bzoj1708】[USACO2007 Oct]Money奶牛的硬币
题目描述 在创立了她们自己的政权之后,奶牛们决定推广新的货币系统.在强烈的叛逆心理的驱使下,她们准备使用奇怪的面值.在传统的货币系统中,硬币的面值通常是1,5,10,20或25,50,以及100单位的 ...
- SQL 谜题(硬币的组合)
问题:早在ITPUB中看过有个SQL高手,喜欢出谜题,以下是一个谜题.我试用SQL SERVER解决此问题. 用1分,5分,10分,25分,50分硬币凑成一元,总共有几种组合办法? SELECT'1* ...
- 洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game
题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...
- [luogu2964][USACO09NOV][硬币的游戏A Coin Game] (博弈+动态规划)
题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...
- 面试题目——《CC150》高等难题
面试题18.1:编写一个函数,将两个数字相加.不得使用+或其他算数运算符. package cc150.high; public class Add { public static void main ...
随机推荐
- CV界的明星人物们
CV界的明星人物们 来自:http://blog.csdn.net/necrazy/article/details/9380151,另外根据自己关注的地方,加了点东西. 今天在cvchina论坛上看到 ...
- 【转】apache kafka监控系列-KafkaOffsetMonitor
apache kafka监控系列-KafkaOffsetMonitor 时间 2014-05-27 18:15:01 CSDN博客 原文 http://blog.csdn.net/lizhitao ...
- linux下gimp的使用
参考资料: http://wenku.baidu.com/view/345c525f804d2b160b4ec070.html 没有视频, 只靠自己摸索使用... 参考文章: http://www.3 ...
- linux shell中判断bash脚本输入的参数个数
看下面的一段程序. #!/bin/bash ]; then echo "参数个数为$#个" else echo "没有参数" fi
- Java字节流:InputStream OutputStream
字节输入流:InputStream 类声明: public abstract class InputStream implements Closeable 位于java.io包下,是一个抽象类. 官方 ...
- linux程序设计1
a.out 的意思是 assembler output,即汇编输出. C语言的头文件一般位于 /usr/include/ 目录下,而依赖于特定 Linux 版本的头文件通常可在目录 /usr/incl ...
- HTML5+学习笔记1-------边看代码边研究中
document.addEventListener('touchstart',function(){ return false; },true); touchstart当手指触摸屏幕时候触发,即使已经 ...
- Maven初级学习(二)Maven使用入门
序,学习配置pom.xml,利用maven生成eclipes项目. 一.编写POM POM Project Obejct Model,项目对象模型. 编写pom.xml,新建文件夹hello-worl ...
- Jquery 操作IFrame
使用jquery操作iframe 1. 内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe id ...
- [C++基础]关于对象的创建及内存分配
测试: #include <stdio.h>#include <QDebug> class KPoint{public: KPoint(int x, int y){ nx = ...