[面试题求解] 两道递归选择题求解
林初茵 2013-02-26 02:53:44 两道都是不定项选择题
10. The recursive function mystrlen(char *buf, int N)defined below tries to find the length of the first null-terminated string in the buffer buf(not counting the null character), where the buffer size is N. For instance if
buf = {'b','u','f','f','e','r','\0','a','b','c'}
with N = 10 is the input, the desired output is 6. If the buffer does not have any null character the desired output is N.
int mystrlen(char *buf, int N)
{
return mystrlen(buf, N/2) + mystrlen(buf + N/2, N/2);
}
What are all the possible mistakes in the code? (代码中可能的错误是哪个/些)
A. There are no mistakes in the code
B. There is no termination of recursion
C. The addition of the the two mystrlen()s in the recursion is incorrect
D. The use of N/2 in the recursion is incorrect
E. Recursion cannot be used to calculate this function
11. Continuing the above example, which of the following recursive implementations fix the code fully? (如果要修正以上的错误,应该)
A. No change to the code in example above
B.
int mystrlen(char *buf int N)
{
if(N==0)
return 0;
else
return mystrlen(buf, N/2) + mystrlen(buf + N/2, N/2);
}
C.
int mystrlen(char *buf int N)
{
if(N==0||buf[0]==0)
return 0;
else if (N==1)
return 1;
int t = mystrlen(buf, N/2);
if(t<N/2)
return t;
else
return (t + mystrlen(buf + N/2, (N+1)/2));
}
D. None of above