- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题 31
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). It is possible to make £2 in the following way: 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p How many different ways can £2 be made using any number of coins?
static int[] nums = {200,100,50,20,10,5,2,1};
static int size = nums.length;
static HashMap<Integer,Integer> pivots = new HashMap<>();
public static int checkSum(HashMap<Integer,Integer> pivots){
int target = 200;
int sum = 0;
for(Integer key: pivots.keySet()){
int pivot = pivots.get(key);
sum += nums[pivot];
if(sum > target) return 1;
}
if(sum < target) return -1;
return 0;
}
public static void shift(HashMap<Integer,Integer> pivots, int pivot_node){
if(pivots.size() + nums[pivots.get(1)] == 201 && pivots.get(1) != 0){
int p_1_value = pivots.get(1); //this part checks whether the current node(which is the first node)
//has reached children of all 1.
//Which means it's time to shift the root node.
pivots.clear();
pivots.put(1 , p_1_value);
shift(pivots, 1);
return;
}
if(pivots.get(pivot_node) != size - 1) {
pivots.put(pivot_node, pivots.get(pivot_node) + 1);
}
else{
shift(pivots , pivot_node - 1);
}
}
public static void branch(HashMap<Integer,Integer> pivots){
pivots.put(pivots.size() + 1, pivots.get(pivots.size()));
}
public static int search(){
int bool = checkSum(pivots);
int n = 0;
int count = 0;
while(n < 25) {
count++;
if (bool == 0) {
n++; // if the sum is equal to 200, we shift the last
//pivot to the next lower number.
shift(pivots, pivots.size());
}else if (bool == -1) {
branch(pivots); //if the sum is less than 200, we make a new pivot with value of the last pivot.
}else if (bool == 1) {
shift(pivots, pivots.size()); //if the sum is greater than 200,
//we shift to the last pivot to the next lower number.
}
bool = checkSum(pivots);
}
return n;
}
public static void main(String[] args){
pivots.put(1,0);
int n = search();
System.out.print("\n\n------------\n\n"+ "n: " + n);
}
这是一种算法,用于搜索合计为目标的集合的组合。这有点像不使用树的深度优先树搜索。每个枢轴代表“树”上的节点。 shift() 方法将节点的值更改为下一个较低的值。 branch() 方法创建一个与上一个节点具有相同值的新节点。 checkSum() 方法检查枢轴的总和是否为 <,= 或 > 目标 200。
方法数的正确答案应该是 73000 左右。但我的算法只返回大约 300 种方法。我不知道为什么会这样,因为我的算法应该达到等于 200 的每一个可能组合。
这是我的算法如何工作的可视化:
最佳答案
您的搜索算法没有找到构成 2 英镑的所有可能的硬币组合,因为您只是将“最后一个支点”移动到下一个较低的数字,而您也应该考虑最后一个之前的项目。
你的算法会找到这个组合:
100, 50, 20, 20, 5, 2, 2, 1
但不是这个:
100, 20, 20, 20, 10, 5, 2, 2, 1
第二个组合中没有值 50,但是您的算法将硬币值从后向分解为仅向前 - 即在以下所有“枢轴”均为 1 之前,它永远不会分解为 50。如果您打印 HashMap<Integer,Integer> pivots
,您可以很容易地看到这一点。每次计数器 n 递增。
您可以尝试通过将代码修改为 shift()
来修复您的代码不仅使用最后一个枢轴,还使用所有不同的先前枢轴。但是,这样做会创建很多重复项,因此您需要保留一份已找到的不同组合的列表。
解决问题 31 的另一种方法是使用动态规划。当涉及到可以分解成更小部分的问题时,动态规划是最好的。例如,同一问题的解决方案,但在哪里 target = 2
可用于解决问题所在 target = 5
,可用于解决 target = 10
的问题等等。
祝你好运!
关于java - 为什么我的搜索算法不适用于 Project Euler 31?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163577/
我正在尝试将欧拉旋转顺序从现有的 xyz 转换为 zxy。谁能帮我做这件事?谢谢。 编辑:我发现这篇非常有用的文章,认为它可以帮助其他人在同一条道路上 - http://knol.google.com
考虑修改后的欧拉问题 #4——“找出最大回文数,它是 100 到 9999 之间的两个数字的乘积。” rev :: Int -> Int rev x = rev' x 0 rev' :: Int ->
例如,我如何在 R 中输入值 e^2? 最佳答案 R 表达式 exp(1) 代表e,并且 exp(2) 代表e^2。 这是有效的,因为 exp 是以 e 为底的求幂函数。 关于R编程: How do
我正在尝试了解 Euler Tour 算法以及为什么它在树遍历中很受欢迎。但是,我看不出 Euler Tour 和树的预序遍历之间的区别。 假设你有一棵树: A / \ B
我一直在尝试对 Euler #22 问题中的这些名称进行排序。我尝试了很多方法来交换字符串。我每次都有问题。有些是随机的符号;有些是随机的。在其他情况下,我在交换时出现了溢出(?)的名字(就像在尝试交
我想为游戏创建一些 physx,我从小例子开始了解它是如何工作的。在此期间我遇到了一些问题,但我在 90% 内解决了它们。 为了创建我的示例,我研究了一些其他示例并创建了我使用的示例:codeflow
/* The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60
我正在研究 Problem 8 of Project Euler我必须从给定的 1000 位数字中找到 13 个连续数字的最大乘积。我将该数字存储在一个文本文件中,然后将其输入到我的源代码中。我的代码
我目前正在尝试解决二体问题,然后我可以升级到更多行星,但它不起作用。它正在输出我不可能的位置。有谁知道是什么原因造成的? 这是我使用的代码: day = 60*60*24 # Constants G
我最近一直在研究毕达哥拉斯三元组和 Euler Bricks并且想知道生成所有这些的最佳方法是什么。 我从更广泛的阅读中知道有 10 个 c n: break
在我当前的 Project Euler problem 5 ,我有一个“有效”的解决方案。它适用于较小的数字(问题中的示例),但不适用于实际问题,因为我在强行使用它,并且程序没有完成。 问题的解释如下
基于标准定义,Eulerian Path是图中的一条路径,它恰好访问每条边一次。 现在,我试图在有向图中找到欧拉路径。我知道欧拉电路的算法。如果一个图有欧拉回路,它就有欧拉路径,这似乎是微不足道的。
我正在研究项目欧拉程序是为了“启蒙”,而不仅仅是解决它们。我已经在 80x80 矩阵上使用动态程序解决了问题 81,但是当我尝试使用统一成本搜索解决它时,我的程序消失在永无止境。我只想知道使用统一成本
我有一个关于 Project Euler 问题和使用循环展开优化的问题。 问题描述:2520是能被1到10的每一个数整除而没有余数的最小数。能被 1 到 20 的所有数字整除的最小正数是多少? 解决方
我正在使用以下行旋转 SCNNode: let rotate = SCNAction.rotateByAngle(CGFloat(M_PI), aroundAxis:SCNVector3Make(0
我想获得更精确的 1/7,但它被截断了。如何在转换有理数时获得更好的精度? >>> str(1.0/7)[:50] '0.142857142857' 最佳答案 Python 有一个用于任意精度计算的内
我正在处理项目 euler 中的问题 401,我在 python 中编写了我的解决方案,但它需要几天时间才能运行,显然我需要加快速度或使用不同的方法。我在 Haskell 中遇到了一个看起来与我的 p
我正在处理 Project Euler 的第四个问题并且遇到了 stackoverflow 异常。我不是在寻求解决问题的帮助,我只是想解释为什么我会收到 stackoverflow 异常。这通常是因为
我已经做了几天 Euler 项目,我不得不承认它很有趣。但是,如果有一些更专注于计算机科学而不是数学的东西,那就太好了。这样的事情对于练习算法和数据结构会很有趣,例如在准备编码面试时。 最佳答案 您可
13195 的质因数是 5、7、13 和 29。 数字 600851475143 的最大质因数是多少? 我自己在Project Euler上解决了这个问题,很慢,后来在某人的github账号上找到了这
我是一名优秀的程序员,十分优秀!