关于递归的问题,尝试安排最大事件数
我正在研究递归,试图更好地利用它。我当前的活动是尝试编写一种递归方法来生成可以安排的最大数量的事件。到目前为止,这是我的方法:
public int maxEvents(int n, int[] Start) {
if(n<=0) return 0;
if(n==1) return 1;
else return maxEvents(n-1, Start) + maxEvents(Start[n]-1, Start);
}
基本上,我的主要方法向 maxEvents 传递一个 int n,这是最后一个事件。假设我有 4 个事件 (1-4),那么 n 将为 4。下一部分是一个数组,索引处的值是事件开始的时间,索引本身是事件结束的时间。
先决条件是:
n >= 0
天,惯例从 1 到 n
事件 n 于(并包括)第 n 天结束,并于 Start[n] 开始
Begin[0] 未使用
对于所有 i从 1 到 n,Begin[i] <= i (当然,约定我的开始日不能晚于结束日。)
最后我的方法应该返回:
如果您必须仅从会议 1 到 n 中进行选择,则可以主办的会议的最大数量。 (如果 n = 0,则此集合为空)
一些示例输入/输出:
n = 3 Begin[1]=1 Begin[2]=1 Begin[3]=3
maxEvents=2
或者:
n = 5 Begin[1]=1 Begin[2]=1 Begin[3]=2 Begin[4]=3 Begin[5]=4
maxEvents=3
我的两次递归调用应该统计最大数量而不邀请n,可以然后从1到n-1中选择。并计算邀请n的最大数量,注意Begin[n]-1是与约定n的开头不冲突的最后一个约定。然后我可以取两者中的最大值,然后返回。
我尝试过使用不同的 if 语句,例如:
if("recursion call 1">"recursion call 2"){ 返回“递归调用1”; 并
使用类似“maxEvents(Start[n]-1, Start)”作为我的递归调用之一(如我上面的代码中使用的),但它没有返回正确的值,就像我上面列出的值一样。总而言之,我在递归的概念上遇到了麻烦,而且我知道我的递归调用出了问题,所以如果有人能指出我正确的方向,那就太好了。谢谢!
I'm working with recursion trying to become better at with it. My current activity is trying to program a recursive method that generates the maximum number of events one could schedule. Heres my method so far:
public int maxEvents(int n, int[] Start) {
if(n<=0) return 0;
if(n==1) return 1;
else return maxEvents(n-1, Start) + maxEvents(Start[n]-1, Start);
}
Basically my main method passes maxEvents an int n, which is the last Event. So say I have 4 events (1-4) then n would be 4. The next part is an array who's value at the index is the time the event starts, and the index itself is when the event ends.
The preconditions are:
n >= 0
days and conventions go from 1 to n
Event n ends at (and includes) day n and begins at Start[n]
Begin[0] is unused
For all i from 1 to n, Begin[i] <= i ( Convention i can't have a later beginning day than ending day, of course.)
At the end my method is supposed to return:
The maximum number of conventions you can host if you must select only from conventions 1 through n. (This set is empty if n = 0)
Some example input / outputs:
n = 3 Begin[1]=1 Begin[2]=1 Begin[3]=3
maxEvents=2
Or:
n = 5 Begin[1]=1 Begin[2]=1 Begin[3]=2 Begin[4]=3 Begin[5]=4
maxEvents=3
My two recursive calls should count the maximum number without inviting n, you can then chose from 1 to n-1. And count the maximum number inviting n, noting that Begin[n]-1 is the last convention that does not conflict with the beginning of convention n. Then I could take the max of the two, and return that.
I've tried using different if statements, saying something like:
if("recursion call 1">"recursion call 2"){
return "recursion call 1";
}
And using something like "maxEvents(Start[n]-1, Start)" as one of my recursive calls (like used in my above code) however its not returning the correct values, like the ones I listed above. All in all I'm having trouble with the concept of recursion, and I know something is wrong with my recursive calls, so if someone could point me in the right direction that'd be great. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有效吗?
这个想法是,您将分成两种互斥的情况:一种是不包含第 n 个事件,另一种是包含第 n 个事件。在两者中,您必须选择更大的。所以将两者相加是不正确的——取最大值才是正确的方法。但您还必须在第二个选项中添加 1 以考虑包括第 n 个事件。
Does this work?
The idea is that you are splitting into two mutually exclusive cases: one where the nth event is not included and another where nth event is included. Out of the two, you have to select the bigger. So adding the two is not correct - taking the max is the right way. But you have to also add a 1 to the second option to account for including the nth event.