c++递归帮助
我在递归方面遇到麻烦。谁能告诉我如何将其转换为代码形式?
给定向量
我想编写一个函数来比较所有值。我现在不关心 1 != 2
是否等同于 2 != 1
。
我在递归方面很糟糕
我保证这不是家庭作业
编辑 我想做的是整理日程表中的事件。我在同一天发生多个事件,我想弄清楚时间表
2 嵌套 for 循环的所有排列都不起作用,因为我正在为每个比较比较多个(> 2)值
event 1 @ 0100-0230, or @ 0200-0330
event 2 @ 1200-1500, or @ 0800-1100
event 3 @ 1200-1300, or @ 1300-1400, or @ 1400-1500
.
.
.
,我想知道是否一组事件相交。我并不是想找到一组全部不相交的事件,
我想得到像这样的输出
event 1 @ 0100-2300, event 2 @ 0800-1100, event 3 @ 1200-1300 // will be printed out
event 2 @ 0200-0330, event 2 @ 1200-1500, event 3 @ 1200-1300 // will be ignored
I am having trouble with recursion. Can anyone show me how to get this into code form?
given vector <int>
with values 1,2,3,4,5,..
i want to write a function that compares all of the value with each other. i dont care about 1 != 2
being equivalent to 2 != 1
for now.
i am so bad at recursion
and i promise that this is not homework
EDIT
what i am trying to do is sort out events of a schedule. i have multiple events happening on the same day and i want to figure out all of the permutations of the schedule
2 nested for loops wont work since i am comparing multiple (>2) values
event 1 @ 0100-0230, or @ 0200-0330
event 2 @ 1200-1500, or @ 0800-1100
event 3 @ 1200-1300, or @ 1300-1400, or @ 1400-1500
.
.
.
for each comparion, i want to find out if that set of events intersect. i am not trying to find a set of events that all do not intersect
i want to get an output like
event 1 @ 0100-2300, event 2 @ 0800-1100, event 3 @ 1200-1300 // will be printed out
event 2 @ 0200-0330, event 2 @ 1200-1500, event 3 @ 1200-1300 // will be ignored
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜你正在研究递归和迭代之间的关系。通常,简单的迭代会转化为尾递归——在这种递归中,递归调用是最后要做的事情,因此不需要深堆栈。
这是一段伪代码,而且还没有经过测试 - 但它应该可以工作。
I guess you're investigating the relationship between recursion and iteration. Normally, simple iteration translates into tail recursion - the kind of recursion where recursive call is the last thing you do, so there is no need to have deep stack.
This is a bit of a pseudocode, plus it's not tested - but it should work.
我敢打赌这是家庭作业......但我不明白为什么你需要递归:
I am betting this is homework... but I don't understand why you need recursion:
为什么需要递归?简单的迭代就可以:
或者您可以使用迭代器:
可以尝试用递归解决问题:
但是,在我看来,非递归解决方案更好。
Why do you need recursion? Simple iteration would do:
Or you can use iterators:
One can try to solve the problem with a recursion:
However, the non-recursive solution is better in my opinion.