- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在练习递归和尝试小问题。我尝试设计的函数之一 comboSum
接受一个整数数组、它的大小和一个目标值。如果任何元素的组合(每个元素只能使用一次)添加到目标值,它返回 true。到目前为止,这是我的实现。
// Expected Behaviors
// comboSum([1, 2, 3], 3, 0) => false
// comboSum([2, 4, 8], 3, 12) => true
// comboSum([2, 4, 8], 3, 11) => false
// comboSum([], 0, 0) => true
bool comboSum(const int a[], int size, int target)
{
if (target == 0)
return true; // if we have decremented target to zero
if (target != 0 && size == 0)
return false; // if we havent hit target to zero and we have no more elements
size--;
return (comboSum(a, size, target) || comboSum(a, size, target - a[size]));
// OR logic ensures that even if just one sum exists, this returns true
}
我的算法有效,但数组具有所有非零值且目标为零的情况除外。该函数立即返回 true,而不检查有效的求和。
// comboSum({1, 2, 3}, 3, 0) returns true when this should be false
有哪些可能的方法可以解决此问题?
编辑:我无法更改参数或调用任何其他辅助函数。函数头必须保持原样,我必须递归地解决问题(不使用 for 或 while 循环或 STL 算法)
最佳答案
一个简单的解决方案是保持 size
参数固定,并有另一个参数指示递归的当前位置。
bool comboSum(const int *a, int size, int pos, int target) {
if (target == 0)
return pos < size || size == 0; // true if pos has moved or if the array is empty
else if (pos == 0)
return false;
pos--;
return comboSum(a, size, pos, target) || comboSum(a, size, pos, target - a[pos]);
}
此解决方案可以正确处理您的测试用例,但我们必须在 size
参数之后传递 pos
参数。两个参数应以相同的值开头:
// comboSum([1, 2, 3], 3, 3, 0) => false
// comboSum([2, 4, 8], 3, 3, 12) => true
// comboSum([2, 4, 8], 3, 3, 11) => false
// comboSum([], 0, 0, 0) => true
由于此问题被标记为 C++,因此最好使用一个比 C 数组更灵活的类。例如,我们可以使用 std::vector
。如果是这样,那么我们就不需要额外的列表大小参数:
#include <vector>
bool comboSum(std::vector<int>& v, size_t pos, int target)
{
if (target == 0) {
return pos < v.size() || v.size() == 0;
} else if (pos == 0) {
return false;
}
pos--;
return comboSum(v, pos, target) || comboSum(v, size, target - v[pos]);
}
关于c++ - 递归确定数组中的元素是否可以求和到目标 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51577015/
我正在尝试在 OCaml 中创建一个函数,该函数在数学中执行求和函数。 我试过这个: sum n m f = if n = 0 then 0 else if n > m then f
我正在尝试找到一个可以帮助我解决问题的公式。 这个公式应该对每个靠近(总是在左边)具有相同名称的单元格的单元格求和(或工作)。如下所示: 将每个大写字母视为 “食谱”并且每个小写字母为 “成分” .在
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我有显示客户来电的数据。我有客户号码、电话号码(1 个客户可以有多个)、每个语音调用的日期记录以及调用持续时间的列。表看起来如下示例。 CusID | PhoneNum | Date
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我得到了两列数据; 答: 2013年12月31日 2013年12月30日 2013年12月29日 2013年12月28日 2013年12月27日 2012年12月26日 B: 10 10 10 10
我对 double 格式的精度有疑问。 示例: double K=0, L=0, M=0; scanf("%lf %lf %lf", &K, &L, &M); if((K+L) 我的测试输入: K
我有以下数组: int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } }; int[,] myArray2 = new int[2, 3
我需要有关报告查询的帮助。我在该方案的底部有一个发票表,需要一种方法来获取总计费金额,同时在此数据库方案中的较高点进行条件过滤。我需要加入其他表,这会导致 SUM 函数返回不需要的结果。 这是我正在使
我有一个使用innodb作为存储引擎的MySQL数据库,并且我有许多采用基本形式的查询: SELECT bd.billing, SUM(CASE WHEN tc.transaction_class
尝试创建一个查询来给出总胜、平和负。我有以下查询 SELECT CASE WHEN m.home_team = '192' AND m.home_full_time_score
我正在尝试生成一份报告,显示排名靠前的推荐人以及他们推荐的人产生了多少收入。 这是我的表格的缩写版本: Users Table ------------------ id referral_user_
我有以下查询,并得到了预期的结果: SELECT IF (a1>b1,'1','0') AS a1r, IF (a2>b2,'1','0') AS a2r,
我尝试了几种不同的解决方案,但都没有成功。我给出的表格是一个示例,其设计和功能与我实际使用的表格类似: PK | Color | Count -------------------
我正在尝试构建一个查询来检查我的库存。 SELECT COUNT(*) AS item_count, reseller_id, sum(sold) as sold_count, sum(refunde
我试图解决一个看起来像下面编写的代码的问题,但由于缺乏知识和阅读 sqlalchemy 文档,我还没有真正找到解决问题的方法。 目标: 如果 year_column 中的年份相同,则获取 sales_
我有一个包含一周中多天的表格。一周中的每一天都有独特的属性,例如冰淇淋是否在这一天成功送达: ID DAY_WEEK ICE_CREAM 1 Monday
首先,我有一个名为store_00的表 id | ref | item | qty | cost | sell 1 22 x1 5 10 15 2 22
我正在编写一个程序,计算每个数字的总和,直到 1000。例如,1+2+3+4+5....+100。首先,我将求和作业分配给 10 个处理器:处理器 0 得到 1-100,处理器 1 得到 101-20
我想在一个循环中一次对多个属性求和: class Some(object): def __init__(self, acounter, bcounter): self.acou
我是一名优秀的程序员,十分优秀!