这个递归函数(回文)变成了一个无限循环
this recursive function(palindrome) become a infinite loop.why?
/*但如果我将第13行更改为"else return回文(s,++f,--l);",则代码运行良好。这是什么原因?*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int palindrome(char* s, int f, int l) // f = first index, l = last index;
{
if(f>=l) return 1;
else if(s[f]!=s[l]) return 0;
else return palindrome(s, f++,l--);
}
int main()
{
char a[100];
gets(a);
int len = strlen(a)-1;
printf("len: %dn",len);
int b = 0;
if(palindrome(a, b , len))
{
printf("Plindromen");
}
else
{
printf("not palindromen");
}
}
签名
int palindrome(char* s, int f, int l)
并调用
f = first index, l = last index;`
错误
++f
/--l
在递归中没有任何作用,除非您将其作为引用参数传递:
int palindrome(char* s, int& f, int& l)
// ^ ^ Add a reference, if you're intending to change
// the parameter value
让我猜猜看。是
palindrome(s, f++, --l)
这意味着对于递归调用回文f永远不会得到增量。它将在函数调用后递增。在你的情况下永远不会。
相关文章:
- 为什么这个递归函数会创建一个无限循环?
- 如何找出为什么我的程序一直有一个无限循环?
- 为什么我有一个无限循环,我如何以不同的方式做到这一点
- WinUsb_WritePipe进入了一个无限循环
- 为什么我会得到一个无限循环(因子)?
- 为什么我总是得到一个无限循环
- 这是一个无限循环吗?程序提前完成
- 为什么它是一个无限循环?
- 为什么我的程序在输入字符时有一个无限循环
- 为什么这段代码最终会进入一个无限循环,读取std::cin
- 如何使其成为一个无限循环
- Tcl_CreateInterp怎么会变成一个无限循环呢
- C++,得到一个无限循环
- C++ 使用 cin.fail(), cin.clear() ---我有一个无限循环,不知道为什么
- 这个递归函数(回文)变成了一个无限循环
- 当对象被卡在一个无限循环中而被破坏时会发生什么
- 代码在小输入时运行良好,但在大输入时就像一个无限循环
- 从文件中读取信息后,会发生一个无限循环
- 数独求解矩阵,而语句给出了一个无限循环
- 为什么这是c++中的一个无限循环