Home > Back-end >  Programmer interview bible recursive problem please help to analysis
Programmer interview bible recursive problem please help to analysis

Time:09-20

When we watch the bible programmer interview to see the following code:
# include
using namespace std;
Int mystrlen (char * buf, int N)
{
If (buf [0]==0 | | N==0)//if the null character, returns 0
return 0;
Else if (N==1)//if the character length is 1, return 1
return 1;
int mystrlen (buf, N/2);//binary recursive length
If (t & lt; N/2)//if the length less than the half of the input N, take the current length

Return t;
Else//instead take the following one character at a time and continue to recursive
Return (t + mystrlen (buf + N/2, (N + 1)/2));
}
Int main ()
{
Char buf []={' a ', 'b', 'c', 'd', 'e', 'f', '\ 0' and 'x', 'y', 'z'};
Int k;
K=mystrlen (buf, 20);
Cout & lt; system("pause");
return 0;
}

There do not understand, int t=mystrlen (buf, N/2) this sentence, why take length?
It looks this place will always recursion, and the return value only can be 0 or 1, judging the result behind (tPlease teach a great god, this place what to understand?
Thank you very much!

CodePudding user response:

Really don't understand, the great god solve, thank you
  • Related