c++递归帮助

发布于 2024-10-24 04:40:14 字数 736 浏览 1 评论 0原文

我在递归方面遇到麻烦。谁能告诉我如何将其转换为代码形式?

给定向量的值为 1,2,3,4,5,..

我想编写一个函数来比较所有值。我现在不关心 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆橘 2024-10-31 04:40:14

我猜你正在研究递归和迭代之间的关系。通常,简单的迭代会转化为尾递归——在这种递归中,递归调用是最后要做的事情,因此不需要深堆栈。

这是一段伪代码,而且还没有经过测试 - 但它应该可以工作。

void compareOne(int compareWith, iterator b, iterator e) {
    if (b == e) return;
    if (compareWith == *b) {
        // do something
    }
    compareOne(compareWith, b+1, e);
}


void compareAll(iterator b, iterator e) {
    if (b == e) return;
    compareOne(*b, b+1, e);
    compareAll(b+1, e);
}

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.

void compareOne(int compareWith, iterator b, iterator e) {
    if (b == e) return;
    if (compareWith == *b) {
        // do something
    }
    compareOne(compareWith, b+1, e);
}


void compareAll(iterator b, iterator e) {
    if (b == e) return;
    compareOne(*b, b+1, e);
    compareAll(b+1, e);
}
深海里的那抹蓝 2024-10-31 04:40:14

我敢打赌这是家庭作业......但我不明白为什么你需要递归:

for(int i = 0; i<v.size(); ++i)
{
   for(int j = i+1; j < v.size(); ++j)
   {
      compare v[i] and v[j]
   }
}

I am betting this is homework... but I don't understand why you need recursion:

for(int i = 0; i<v.size(); ++i)
{
   for(int j = i+1; j < v.size(); ++j)
   {
      compare v[i] and v[j]
   }
}
带刺的爱情 2024-10-31 04:40:14

为什么需要递归?简单的迭代就可以:

using namespace std;
vector<int> v;
...
for (int i = 0; i < v.size() - 1; i++)
    for (int j = i + 1; j < v.size(); j++)
        if (v[i] == v[j])
            cout << "Oops" << endl;

或者您可以使用迭代器:

vector::const_iterator beforelast = v.end(); --beforelast;
for (vector::const_iterator i = v.begin(); i != beforelast; ++i)
    for (vector::const_iterator j = i + 1; j != v.end(); ++j)
        if (*i == *j)
            cout << "Oops" << endl;

可以尝试用递归解决问题:

bool f(vector<int>& v, int lastidx)
{
    int lastval = v[lastidx];
    for (int i = 0; i < lastidx; i++)
        if (v[i] == lastval)
            return false;
    return f(v, lastidx - 1);
}

但是,在我看来,非递归解决方案更好。

Why do you need recursion? Simple iteration would do:

using namespace std;
vector<int> v;
...
for (int i = 0; i < v.size() - 1; i++)
    for (int j = i + 1; j < v.size(); j++)
        if (v[i] == v[j])
            cout << "Oops" << endl;

Or you can use iterators:

vector::const_iterator beforelast = v.end(); --beforelast;
for (vector::const_iterator i = v.begin(); i != beforelast; ++i)
    for (vector::const_iterator j = i + 1; j != v.end(); ++j)
        if (*i == *j)
            cout << "Oops" << endl;

One can try to solve the problem with a recursion:

bool f(vector<int>& v, int lastidx)
{
    int lastval = v[lastidx];
    for (int i = 0; i < lastidx; i++)
        if (v[i] == lastval)
            return false;
    return f(v, lastidx - 1);
}

However, the non-recursive solution is better in my opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文