简单的递归问题

发布于 2024-09-12 20:36:50 字数 587 浏览 3 评论 0原文

假设我们有一个简单的递归。

int x(int a){
   if(a<10)
     x(a+1);
    else
      !STOP!
    b++;
return b;
}

Globaly:

int b=0;

在 main 中我们可以有这样的东西:

  int p=x(1);

有没有办法停止递归,使 p 为 0,这意味着“b++”永远不会被执行。

如果您能告诉我一些代替 !STOP! 的表达式,我将不胜感激。

但是,我不想要这样的事情,我只想停止递归,比如break;在 while() 循环中执行...:

int ok=0;
  int x(int a){
       if(a<10)
         x(a+1);
        else
          ok=1;
      if(ok==0)
        b++;
    return b;
    }

如果对问题有任何不清楚的地方,请询问。

Let's say we have a simple recursion like.

int x(int a){
   if(a<10)
     x(a+1);
    else
      !STOP!
    b++;
return b;
}

Globaly:

int b=0;

In main we could have something like this:

  int p=x(1);

Is there any way to stop the recursion so that the p will be 0, this means that "b++" will never be executed.

I'll be grateful if you could tell me some expresion to put instead of the !STOP!

But, I don't want anything like this, I just want to stop the recursion, like break; does in a while() loop...:

int ok=0;
  int x(int a){
       if(a<10)
         x(a+1);
        else
          ok=1;
      if(ok==0)
        b++;
    return b;
    }

If there's anything unclear about the question, just ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

原谅我要高飞 2024-09-19 20:36:50

你为什么不这样做呢?

int x(int a){
   if(a<10) {
      x(a+1);
      b++;
   }
   return b;
}

但问题是,您正在递归例程中修改全局变量,这并不是特别线程安全并且相当草率。您返回的值始终被忽略,除了顶级调用者之外。您还正在做一些最好在循环中完成的事情(但我假设您的实际情况比这更大,或者您是一名学生)。

你不能真正“打破”递归——返回足够好。在老式 C 中,您可能会使用 setjmp/longjmp (及其所有危险 - 换句话说,不要),而在 C++ 中,您可能会使用 try/catch/ throw,这也会展开堆栈。

Why wouldn't you do this?

int x(int a){
   if(a<10) {
      x(a+1);
      b++;
   }
   return b;
}

The thing is, though, you're modifying a global in a recursive routine, which is not especially threadsafe and pretty sloppy. You're returning a value that is always ignored except by the top level caller. You're also doing something that is better off being done in a loop (but I assume that your actual case is bigger than this, or you're a student).

You can't really "break" the recursion - returning unwinds well enough. In oldey-timey C you might use setjmp/longjmp (and all its perils - in other words, DON'T), and in C++ you might use try/catch/throw, which will unwind the stack as well.

缪败 2024-09-19 20:36:50

像这样怎么样?

int x(int a){
   if(a>0 && a<10)
     x(a+1);
   b++;
   return b;
}

How about like this?

int x(int a){
   if(a>0 && a<10)
     x(a+1);
   b++;
   return b;
}
习ぎ惯性依靠 2024-09-19 20:36:50

C++ 中唯一能像这样展开堆栈的就是异常。还有 setjmp()/longjmp(),但它们永远不应该在 C++ 程序中使用。任何其他构造最多可以从当前函数返回。

The only thing in C++ that will unwind the stack like that is an exception. There's also setjmp()/longjmp(), but those should never be used in a C++ program. Any other construct can at most return from the current function.

四叶草在未来唯美盛开 2024-09-19 20:36:50

回来怎么样?

int x(int a){
   if(a<10)
     x(a+1);
    else
      return b;
    b++;
return b;
}

我认为这看起来更好一点

int x(int a){
   if(a<10)
     x(a+1);
    else
      return b;

    return ++b;
}

编辑:

我认为您可以使用异常机制来展开堆栈并到达第一次调用点,但输入main()。在x中引用b,给定代码:

int b = 0;
int p = x(1);

表明x用于初始化某些全局变量,并且可以在main之前执行()。如何使用一些辅助函数,将 x 的调用包装在 try - catch 块中,并在 |STOP| 的位置引发异常?

How about returning?

int x(int a){
   if(a<10)
     x(a+1);
    else
      return b;
    b++;
return b;
}

I think this looks a bit better

int x(int a){
   if(a<10)
     x(a+1);
    else
      return b;

    return ++b;
}

EDIT:

I think You could use exception mechanism to unwind the stack and get to the point of first invocation, but it's safe after entering main(). Referencing b in x, given the code:

int b = 0;
int p = x(1);

suggests that x is used for initialization of some global variable and may be executed before main(). How about using some helper function that wraps invocation of x in a try - catch block and throwing an exception in the place of |STOP|?

半夏半凉 2024-09-19 20:36:50

如果您尝试在 main() 中声明 b,并在 x() 中使用 b,则有一开始就已经出了问题。相反,通过将 b 作为参数传递给 x 并返回 b 的修改版本,使 b 成为局部变量。

int x(int a, int b){
   if(a<10)
      return x(a+1,b+1);
    else
      return b;
}

If you're trying to declare b in main(), and use b in x() then there's something wrong already to begin with. Instead, make b into a local variable by passing it as a parameter to x, and returning a modified version of b.

int x(int a, int b){
   if(a<10)
      return x(a+1,b+1);
    else
      return b;
}
哎呦我呸! 2024-09-19 20:36:50

我不太喜欢使用异常进行控制。我不希望通过使用异常而不是 if/return 语句来节省很多周期。无论如何,在抛出异常之前,您都必须测试边界条件。

但是,您可以通过更改函数的返回类型来稍微简化问题。

bool x(int a){
  if(ok) //Exit early before next call up?
    return true;  
  if(a<10){
    if(x(a+1))  //Have we been told to exit early?
      return true; //Yes
    b++; //Do some work
    if(ok) //Exit early in the next call down?
      return true;  
  }
  return false; //Normal Exit
}

I'm not a big fan of using an Exception for control. I don't expect you'll save many cycles by using Exceptions instead of if/return statements. You're going to have to test your boundary conditions anyway before throwing an Exception.

You can however simplify the problem a bit by changing the return type of the function.

bool x(int a){
  if(ok) //Exit early before next call up?
    return true;  
  if(a<10){
    if(x(a+1))  //Have we been told to exit early?
      return true; //Yes
    b++; //Do some work
    if(ok) //Exit early in the next call down?
      return true;  
  }
  return false; //Normal Exit
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文