- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的回溯函数有问题,它循环处理某些数据我不能在这里写整个程序代码,但可以把我的函数放在这里。
bool shareMoney(int quantity, int *values, int index, int moneyA, int half, bool *ifChosen)
{
if(moneyA == half)
return true;
else if(index >= quantity)
return false;
moneyA += values[index];
if(shareMoney(quantity, values,index+1, moneyA, half, ifChosen))
{
ifChosen[index] = true;
return true;
};
moneyA -= values[index];
if(shareMoney(quantity, values,index+1, moneyA, half, ifChosen))
{
ifChosen[index] = false;
return true;
};
return false;
}
下面是解释:
数量 - 值数组中元素的数量
值 - 数字数组
index - 控制递归的变量
moneyA - 存储数组值元素总和的变量
half - 递归完成后 moneyA 应该达到的数字
ifChosen - 引用数组值的 bool 元素数组
该函数获取元素的数量,它是 values 数组的长度,values 是一个包含数字的数组,从最高到最低排序,index 控制递归,默认为 0,因此它从第一个元素 moneyA 变量开始存储值数组中的数字,它应该达到一半,即从值数组中求和的数字的一半,ifChosen 存储选择的数字。
整个函数就是这样做的,它对 values 数组中的元素求和并检查它是否达到了一半,如果它低于一半,将它添加到 moneyA 并在 ifChosen 中标记它然后它转到下一个,如果总和更高它返回一半以上并在 ifChosen 数组中取消标记并从 moneyA 中减去。它应该总是获得最高的元素。
这是一个简单的例子:
6 - number of elements
50, 20, 19, 15, 2, 2 - elements stored in values array
total sum is - 108
half of elements - 54
这个结果应该是:
50, 2, 2 - marked as true in ifChosen
20, 19, 15 - marked as false in ifChosen
当然,对于这个简单的例子,它做得很好,但对于更复杂的例子,有更多的数字,一个数字出现不止一次,它会循环,递归永远不会停止。我实际上已经为此工作了 1.5 周,并询问了我所有的 friend ,但没有人知道它出了什么问题。我知道这与背包问题有点关系,但我还没有那个,仍然需要学习。
我期待任何可能有所帮助的答案。
对不起我的标点符号,但我是第一次来这里,不习惯格式。
举个例子:
89 86 83 67 53 45 5 1
44 43 34 33 33 24 23 23 23 22 21 21 19 12 11 9 8 7 5 3 2 2 2 1 1 1 1 1
real 0m28.007s
user 0m27.926s
sys 0m0.028s
现在我认为它会永远循环:43个元素:
12 2 2 1 3 4 5 6 7 89 33 12 45 23 44 23 11 44 1 3 5 7 88 33 8 19 43 22 86 5 34 23 21 67 83 24 21 53 9 11 34 1 1
@Karl Bielefeldt 是的,我知道有如此多的组合,这就是我试图加快速度的原因。现在这就是我所得到的,但它给了我某些输入的错误结果。任何人都可以纠正它,它的工作速度比以前快得多吗?
bool shareMoney(int quantity, int *values, int index, int moneyA, int half, bool *ifChosen){
if(index>=quantity && moneyA == half){ return true;}
else if(index>=quantity) return false;
moneyA+=values[index];
ifChosen[index]=true;
if(moneyA<=half){
shareMoney(quantity,values,index+1,moneyA, half,ifChosen);
if(moneyA==half) return true;
return true;
}else{
shareMoney(quantity,values,index+1,moneyA, half,ifChosen);
moneyA-=values[index];
ifChosen[index]=false;
return true;
}
return false;}
最佳答案
减少此类问题的迭代次数的典型方法是通过求解线性程序来计算子树的边界(就像您的问题,但允许其余变量采用分数值)。 Simplex 在近似二次时间而不是指数时间内求解线性程序。线性规划的最佳解决方案至少与具有相同约束的最佳整数或二进制解决方案一样好,因此如果线性解决方案比您当前的最佳解决方案差,您可以在不进行详尽评估的情况下丢弃整个子树。
编辑:让我们从简化蛮力算法开始:
int* shareMoney( int pool_size, int *pool, int *solution, int cumsum, int goal)
{
if (cumsum == goal) return solution;
#if PRUNE_ABOVE
if (cumsum > goal) return 0;
#endif
if (!pool_size) return 0;
#if PRUNE_BELOW
int max = cumsum;
for( int n = pool_size; n--; max += pool[n] );
if (max < goal) return 0;
#endif
int* subproblem = shareMoney(pool_size-1, pool+1, solution, cumsum, goal);
if (subproblem) return subproblem;
*solution = *pool;
return shareMoney(pool_size-1, pool+1, solution+1, cumsum+*pool, goal);
}
执行后,solution
包含一个用于达到目标的值的列表,返回的指针指示列表的结尾。
条件 block 是我第一个建议的改进。在这些情况下不需要递归。
我们可以消除在每一步迭代计算最大值的需要:
int* shareMoney( int pool_size, int *pool, int *solution, int cumsum, int poolsum, int goal)
{
if (cumsum == goal) return solution;
#if PRUNE_ABOVE
if (cumsum > goal) return 0;
#endif
if (!pool_size) return 0;
#if PRUNE_BELOW
if (cumsum + poolsum < goal) return 0;
#endif
int* subproblem = shareMoney(pool_size-1, pool+1, solution, cumsum, poolsum - *pool, goal);
if (subproblem) return subproblem;
*solution = *pool;
return shareMoney(pool_size-1, pool+1, solution+1, cumsum+*pool, poolsum - *pool, goal);
}
这是一个解决整数版本的函数(对于重复的硬币面额更好):
int* shareMoney( int pool_size, int *pool_denom, int *pool_cardinality, int *solution, int cumsum, int poolsum, int goal)
{
if (cumsum == goal) return solution;
#if PRUNE_ABOVE
if (cumsum > goal) return 0;
#endif
if (!pool_size) return 0;
#if PRUNE_BELOW
if (cumsum + poolsum < goal) return 0;
#endif
poolsum -= *pool_cardinality * *pool_denom;
for (*solution = *pool_cardinality; *solution >= 0; --*solution) {
int* subproblem = shareMoney(pool_size-1, pool_denom+1, pool_cardinality+1, solution+1, cumsum + *solution * *pool_denom, poolsum, goal);
if (subproblem) return subproblem;
}
return 0;
}
它不是获取单个硬币的直接列表,而是获取面额列表以及每个硬币的可用硬币数量。结果是解决方案所需的每种面额的硬币数量。
关于c++ - 递归回溯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5096414/
在本教程中,您将借助示例了解 JavaScript 中的递归。 递归是一个调用自身的过程。调用自身的函数称为递归函数。 递归函数的语法是: function recurse() {
我的类(class) MyClass 中有这段代码: public new MyClass this[int index] { get {
我目前有一个非常大的网站,大小约为 5GB,包含 60,000 个文件。当前主机在帮助我将站点转移到新主机方面并没有做太多事情,我想的是在我的新主机上制作一个简单的脚本以 FTP 到旧主机并下载整个
以下是我对 AP 计算机科学问题的改编。书上说应该打印00100123我认为它应该打印 0010012但下面的代码实际上打印了 3132123 这是怎么回事?而且它似乎没有任何停止条件?! publi
fun fact(x: Int): Int{ tailrec fun factTail(y: Int, z: Int): Int{ if (y == 0) return z
我正在尝试用c语言递归地创建线性链表,但继续坚持下去,代码无法正常工作,并出现错误“链接器工具错误 LNK2019”。可悲的是我不明白发生了什么事。这是我的代码。 感谢您提前提供的大力帮助。 #inc
我正在练习递归。从概念上讲,我理解这应该如何工作(见下文),但我的代码不起作用。 请告诉我我做错了什么。并请解释您的代码的每个步骤及其工作原理。清晰的解释比只给我有效的代码要好十倍。 /* b
我有一个 ajax 调用,我想在完成解析并将结果动画化到页面中后调用它。这就是我陷入困境的地方。 我能记忆起这个功能,但它似乎没有考虑到动画的延迟。即控制台不断以疯狂的速度输出值。 我认为 setIn
有人愿意用通俗易懂的语言逐步解释这个程序(取自书籍教程)以帮助我理解递归吗? var reverseArray = function(x,indx,str) { return indx == 0 ?
目标是找出数组中整数的任意组合是否等于数组中的最大整数。 function ArrayAdditionI(arr) { arr.sort(function(a,b){ return a -
我在尝试获取 SQL 查询所需的所有数据时遇到一些重大问题。我对查询还很陌生,所以我会尽力尽可能地描述这一点。 我正在尝试使用 Wordpress 插件 NextGen Gallery 进行交叉查询。
虽然网上有很多关于递归的信息,但我还没有找到任何可以应用于我的问题的信息。我对编程还是很陌生,所以如果我的问题很微不足道,请原谅。 感谢您的帮助:) 这就是我想要的结果: listVariations
我一整天都在为以下问题而苦苦挣扎。我一开始就有问题。我不知道如何使用递归来解决这个特定问题。我将非常感谢您的帮助,因为我的期末考试还有几天。干杯 假设有一个包含“n”个元素的整数数组“a”。编写递归函
我有这个问题我想创建一个递归函数来计算所有可能的数字 (k>0),加上数字 1 或 2。数字 2 的示例我有两个可能性。 2 = 1+1 和 2 = 2 ,对于数字 3 两个 poss。 3 = 1+
目录 递归的基础 递归的底层实现(不是重点) 递归的应用场景 编程中 两种解决问题的思维 自下而上(Bottom-Up) 自上而下(Top-
0. 学习目标 递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。本节主要介绍递归的基本概念以及如何构建递归程序。
我有一个问题一直困扰着我,希望有人能提供帮助。我认为它可能必须通过递归和/或排列来解决,但我不是一个足够好的 (PHP) 程序员。 $map[] = array("0", "1", "2", "3")
我有数据 library(dplyr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg, qsec) %>% h
在 q 中,over 的常见插图运算符(operator) /是 implementation of fibonacci sequence 10 {x,sum -2#x}/ 1 1 这确实打印了前 1
我试图理解以下代码片段中的递归调用。 static long fib(int n) { return n <= 1 ? n : fib(n-1) + fib(n-2); } 哪个函数调用首先被
我是一名优秀的程序员,十分优秀!