无法读取此递归函数 [英] Can't read this recursive function
本文介绍了无法读取此递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我正在使用这段代码。
我不知道怎么读它,尤其是'|| 在函数的两个召唤之间。
谢谢。
Hi guys
i'm having truoble with this code.
i dont know how to read it, especially the ' || ' between the two summonus of the function.
thanks.
#include <stdio.h>
int f3(int *a, int n, int m);
int f4(int *a, int n);
void f5(int *a, int n);
int main()
{
int arr[6] = {3, 12, 6, 11, 2, 10};
char *txt[] = {"NO","YES","MAYBE","PROBABLY"};
printf("%s\n",txt[f3(arr,6,5)]);
return 0;
}
int f3(int *a, int n, int m)
{
if(m==0) return 1;
if(n<2) return 0;
return f3(a+1,n-1,m-a[0]) || f3(a+1,n-1,m);
}
推荐答案
除了上述内容之外,以下修改可能有助于更好地理解该问题。我相信'enum'在C和C ++中的作用相同。
In addition to the above, perhaps the following reworking helps better understand the issue. I believe 'enum' works the same in C as C++.
#include <stdio.h>
enum Branch { InitialEntry, LeftBranch, RightBranch };
int f3(int *a, int n, int m, Branch b);
int main()
{
int arr[6] = {3, 12, 6, 11, 2, 10};
char *txt[] = {"NO","YES","MAYBE","PROBABLY"};
printf("%s\n",txt[f3(arr,6,5, InitialEntry)]);
return 0;
}
int f3(int *a, int n, int m, Branch b)
{
if(b==InitialEntry) printf("%s %d %d\n", "Entry ",n, m);
else if(b==LeftBranch) printf("%s %d %d\n", "Left ",n, m);
else printf("%s %d %d\n", "Right ",n, m);
if(m==0) return 1;
if(n<2) return 0;
return f3(a+1,n-1,m-a[0], LeftBranch) || f3(a+1,n-1,m, RightBranch);
}
||
运算符是逻辑OR
运算符,如 http:// msdn中所述。 microsoft.com/en-us/library/x04xhy0h(v=VS.80).aspx [ ^ ]。在上面的代码中,return
语句将返回两次调用f3
函数的逻辑和。至于这个功能实际上做了什么,我没有分析它,所以不能告诉你。
The||
operator is the logicalOR
operator as described in http://msdn.microsoft.com/en-us/library/x04xhy0h(v=VS.80).aspx[^]. In the above code thereturn
statement will return the logical sum of the two calls to thef3
function. As to what this function actually does, I have not analysed it so cannot tell you.
这篇关于无法读取此递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文