【发布时间】:2019-04-20 20:25:06
【问题描述】:
我正在寻找一个问题的解决方案,我已经在 c++ 中找到了解决方案,但是当我在 python 中尝试相同的逻辑时,它给出了RecursionError: maximum recursion depth exceeded in comparison。
x=2
y=500
#Python Implementation
def F(x,y):
if(x==0):
return (y+1)%1000
if(x>0 and y==0):
return F(x - 1, 1)%1000
else:
return F(x - 1, F(x, y - 1))
print(str(F(x,y)))
#C++ Implementation
int f(int x,int y)
{
if(x==0)
return (y+1)%1000;
if(x>0&&y==0)
return f(x-1,1)%1000;
else
return f(x-1,f(x,y-1));
}
int main()
{
int x,y;
scanf("%d%d",&x,&y);
printf ("%03d", f(x,y));
return 0;
}
提前致谢。
【问题讨论】:
-
在不相关的注释中,“C++”代码中没有任何特定于 C++ 的内容,它可能是一个普通的 C 程序。在更相关的说明中,这两个程序并不完全相同:函数中的条件不一样(
elif与else)。 -
更新请检查
标签: c++ python-3.x recursion