无穷大而循环时具有递归函数
infinity while loop when have recursive function
我正在解决一个问题。我想要一个函数,它返回所有方法,使不同的正数int
加上等于该数字,例如 6 将是 1+5 ,2+3+1,2+4 所以将是 3 但我的解决方案返回无限循环
#include <iostream>
using namespace std;
int find(int num,int before)
{
int first=1;
int count=1;
int end =num-1;
if(end-first==0) return count;
while(end-first!=1&&end-first!=0)
{
if(end==before||first==before) continue;
first++;
end--;
}
before=first;
return count+find(end,before);
}
int main()
{
int a;
cin>>a;
int x=find(a,1);
cout<<x;
}
我尝试循环cout
"a"并永远重复。请帮助我。
编辑:我的代码只是解决了一块问题,所以它不是解决方案,我会尝试关闭主题,谢谢大家
在这个概念中,first
将始终为 1,before
将始终为1,因为此处的first
永远不会递增,因为未达到指令。
first
由1初始化,before
由1初始化,这意味着before==first
为 true,循环将在continue
后忽略所有其他指令。
既然你比较end==before||first==before
这永远是正确的,因为即使end==before
false
第二个测试也是真的。false||true
的测试逻辑是true
。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何在Elixir中调用递归函数并行
- 递归函数有效,但无法记忆
- 为什么我的递归函数按降序打印,然后按升序打印?
- 为什么递归函数的最终输出是 5?
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 无穷大而循环时具有递归函数
- 为什么这个递归函数会创建一个无限循环?
- C 递归函数无限循环
- 何时在此递归函数中发生for循环增量
- 实现递归函数,避免由 C++ 中 include 的循环调用(没有 #pragma 一次)引起的无限循环输入
- 如何使递归函数在进入下一级循环之前读取每个新实例
- Python 3.3 如何将这个递归函数转换为纯收益循环版本?
- 哪个是有效的:递归函数或 for 循环
- 在循环中调用递归函数
- 这个递归函数(回文)变成了一个无限循环
- 带有for循环的递归函数:返回值问题
- Scala/ c++:尾部递归函数代替输入循环
- C++ |需要帮助从 for 循环函数转换 - >递归函数
- 在c++中使用递归函数循环遍历deque